CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

Similar documents
2 What are the differences between constructors and methods?

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

CS 113 MIDTERM EXAM 2 SPRING 2013

Object-Oriented Programming Concepts

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

University of Palestine. Mid Exam Total Grade: 100

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Final Exam. COMP Summer I June 26, points

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Questions Answer Key Questions Answer Key Questions Answer Key

COMPUTER APPLICATIONS

CS 113 PRACTICE FINAL

Practice Questions for Chapter 9

AP CS Fall Semester Final

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

CS111: PROGRAMMING LANGUAGE II

Lecture 5: Methods CS2301

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Computer Science is...

Introduction to Programming Using Java (98-388)

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

COMPUTER APPLICATIONS

Questions Answer Key Questions Answer Key Questions Answer Key

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

Give one example where you might wish to use a three dimensional array

Question: Total Points: Score:

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Use the scantron sheet to enter the answer to questions (pages 1-6)

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Lecture #6-7 Methods

Example: Computing prime numbers

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

H212 Introduction to Software Systems Honors

Questions Answer Key Questions Answer Key Questions Answer Key

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Introduction to Programming (Java) 4/12

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Question: Total Points: Score:

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Computer Science II (20082) Week 1: Review and Inheritance

CS 101 Spring 2007 Midterm 2 Name: ID:

Computer Programming, I. Laboratory Manual. Final Exam Solution

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

CS 177 Week 15 Recitation Slides. Review

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Arrays. Eng. Mohammed Abdualal

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Selected Questions from by Nageshwara Rao

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Lecture #8-10 Arrays

CS170 Introduction to Computer Science Midterm 2

CS212 Midterm. 1. Read the following code fragments and answer the questions.

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Java Identifiers, Data Types & Variables

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

Building Java Programs

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Last Class. While loops Infinite loops Loop counters Iterations

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 1: Introduction to Computers, Programs, and Java

Computer Science 1 Ah

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CSCE 2014 Final Exam Spring Version A

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CS-201 Introduction to Programming with Java

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Types, Values and Variables (Chapter 4, JLS)

ITI Introduction to Computing II

Zheng-Liang Lu Java Programming 45 / 79

ITI Introduction to Computing II

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Introduction to Programming (CS112): Sample

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Transcription:

CS1 Part 2: Algorithms on Arrays, Classes and Objects, String Class, Stack Class 1. Write a method that, given two sorted arrays of integers, merges the two arrays into a single sorted array that is returned. 2. Write a method that, given an array of integers, computes the sum of every other number (starting at the zero th index) and returns true if the sum is digisible by and false otherwise. 3. What is the output of the following program lines when they are embedded in a correct Java program. 3 int number = 0; 4 int[] numbers = new int[1]; 5 numbers[0] = 0; 6 m(number, numbers); 7 System.out.println("number is " + number 8 + " and numbers[0] is " + numbers[0]); 9 11 public static void m(int x, int[] y) { 12 x = 3; 13 y[0] = 3; 14 15 16 4. Write the following method that returns true if the list is already sorted in increasing order and false if it is not sorted. public static boolean issorted(int{[{] list) 5. Write a method that given a list of floating point numbers determines if a particular value is on the list. Your method should return the location of the item if it is found, or -1 if it is not found. Use the following method header: public static int find(double {[{] list, double key) Use the searching algorithm of your choice. 6. What is wrong with the following program or code fragment? How would you correct the errors? (a) assume that the Circle class has a default constructor and getradius method public class ShowErrors { public void method1() { Circle c; System.out.println("What is radius " + c.getradius()); c = new Circle(); (b) public class ShowErrors { public static void main(string[] args) { C c = new C(5.0); System.out.println(c.value); class C { int value = 2; Page 1

(c) public class Test { public static void main(string[] args) { A a = new A(); a.print(); class A { private String s; A(String s) { this.s = s; public void print() { System.out.print(s); 7. If all objects of a class need to share the same variable, how must you declare that variable? Show an example of such definition. 8. To make a data field or a method accessible by only other members of its class, what access modifier must be used? 9. Can you invoke an instance method or reference an instance variable from a static method? Can you invoke a static method or reference a static variable from an instance method?. What is the difference between a class and an object? 11. What does each object have its own copy of? 12. What name does a constructor have? 13. What is wrong with the following program or code fragment? How would you correct the errors? (a) assume that the Circle class has a default constructor public class Foo { public static void method0() { method1(); public void method1() { method2(); public static void method2() { System.out.println("What is radius " + c.getradius()); Circle c = new Circle(); (b) public class C { private int p; public C() { C(0); public C(int p) { p = p; public void setp(int p) { p = p; Page 2

14. What does the new operator do? 15. What is the this reference used for? Describe all of its uses. 16. Can a constructor have one or more parameters? Can a class have more than one constructor? 17. Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Give examples of both the primitive types and the reference types. 18. Write a method that, given an array of Circle objects, sorts the circles from smallest to largest. Assume that the Circle class is defined as follows: public class Circle { private float radius; public Circle ( float r ) { if (r >=0 ) radius = r; else radius = 1; public float getradius() { return radius; public int compareto( Circle c ) { if (radius == c.radius ) return 0; else if (radius < c.radius ) return -1; else return 1; You should use the following signature for the method: void sortcircles ( Circle [] circles ) You can use any sorting algorithm we discussed. 19. Suppose that s1 and s2 are two instances of the Java String class. Which of the following statements or expressions are incorrect? (a) String s = new String("new string"); (b) String s3 = s1 + s2; (c) String s3 = s1 - s2; (d) s1 == s2; (e) s1 >= s2; (f) s1.compareto(s2); (g) int i = s1.length(); (h) char c = s1(0); (i) char c = s1.charat(s1.length()); 20. Suppose that the class Foo is defined as shown. Let f be an instance of Foo. Which of the statements below are invalid? public class Foo { int i; static String s; void imethod() {... static void smethod() {... Page 3

(a) System.out.println(f.i); (b) System.out.println(f.s); (c) f.imethod(); (d) f.smethod(); (e) System.out.println(Foo.i); (f) System.out.println(Foo.s); (g) Foo.imethod(); (h) Foo.smethod(); 21. Write a method that given a string object that consists of digits, computes and returns the sum of every other digit (starting at the very first one). The string of digits can be arbitrary length, but you may assume that the sum of the digits fits in a variable of type int. 22. Write the Java function lengthofsortedsequence which returns the length of the longest sorted sequence in an array. For this problem a sorted sequence is a sequence of non-decreasing values. Here are some examples: array return value from legnthofsortedsequence(array) [-7 3 99-0 0 43 20 30 5] 4 [-1, 2, 3, 3] 4 [5, 2, 1] 1 [1] 1 [] 0 23. In the following class, the variable n occurs in multiple scopes. Which declarations of n are legal and which are illegal? 1 public class X 2 { 3 private int n; 4 5 public int f() 6 { 7 int n = 1; 8 return n; 9 11 public int g(int k) 12 { 13 int a; 14 for (int n = 1; n <= k; n++) 15 a = a + n; 16 return a; 17 18 public int h(int n) 19 { 20 int b; 21 for (int n = 1; n <= ; n++) 22 b = b + n; 23 return b + n; 24 25 26 public int k(int n) 27 { 28 if (n < 0) 29 { 30 int k = -n; 31 int n = (int) (Math.sqrt(k)); 32 return n; Page 4

33 34 else return n; 35 36 37 public int m(int k) 38 { 39 int a; 40 for (int n = 1; n <= k; n++) 41 a = a + n; 42 for (int n = k; n >= 1; n++) 43 a = a + n; 44 return a; 45 46 24. Show the output of the following programs: (a) 3 Count mycount = new Count(); 4 int times = 0; 5 for (int i = 0; i < 0; i++) 6 increment(mycount, times); 7 System.out.println("count is " + mycount.count); 8 System.out.println("times is " + times); 9 11 public static void increment(count c, int times) { 12 c.count++; 13 times++; 14 15 1 public class Count { 2 public int count; 3 public Count(int c) { 4 count = c; 5 6 public Count() { 7 count = 1; 8 9 11 12 13 14 15 (b) 3 T t1 = new T(); 4 T t2 = new T(); 5 System.out.println("t1 s i = " + 6 t1.i + " and j = " + t1.j); 7 System.out.println("t2 s i = " + 8 t2.i + " and j = " + t2.j); 9 1 class T { 2 static int i = 0; 3 int j = 0; 4 T() { 5 i++; 6 j = 1; 7 8 9 (c) 3 int[] a = {1, 2; 4 swap(a[0], a[1]); 5 System.out.println("a[0] = " + a[0] 6 + " a[1] = " + a[1]); 7 8 public static void swap(int n1, int n2) { 9 int temp = n1; n1 = n2; 11 n2 = temp; 12 13 Page 5

(d) 3 int[] a = {1, 2; 4 swap(a); 5 System.out.println("a[0] = " + a[0] 6 + " a[1] = " + a[1]); 7 8 public static void swap(int[] a) { 9 int temp = a[0]; a[0] = a[1]; 11 a[1] = temp; 12 13 25. Consider the following code. 1 public class Ball 2 { 3 private double rx, ry; 4 public Ball() 5 { 6 rx = 0.5; 7 ry = 0.5; 8 9 public void move() { 11 rx = rx +.001; 12 ry = ry +.002; 13 14 public void move(double vx, double vy) 15 { 16 rx = rx + vx; 17 ry = ry + vy; 18 19... 20 public static void main(string[] args) 21 { 22 private double vx, vy; 23 Ball b1 = new Ball(); 24 Ball b2 = new Ball(); 25 b1.move(); 26... 27 b2.move(vx, vy); 28... 29 30 In the blanks at left, give all line numbers that contain each of the entities described at right. (a) (b) (c) (d) (e) (f) (g) instance variable declaration constructor signature object creation overloaded method signature method invocation or call primitive type variable declaration reference type variable declaration 26. What is printed? (a) Show the output of the following program: Page 6

3 Counter mycounter = new Counter(); 4 int times = 0; 5 6 for (int i = 0; i < 0; i++) 7 increment(mycounter, times); 8 9 System.out.println("counter is " + mycount.count); System.out.println("times is " + times); 11 12 13 public static void increment(count c, int times) { 14 c.count++; 15 times++; 16 17 18 19 public class Counter { 20 public int count; 21 22 Counter() { 23 count = 0; 24 25 (b) Show the output of the following program: 3 int[][] array = {{1, 2, 3, {4, 5, 6, 4 { 7, 8, 9, {, 11, 12 ; 5 System.out.println( array.length ); 6 System.out.println( array[0].length ); 7 8 27. Write a method that given a two dimensional array of real numbers creates and returns a one dimensional that contains sums of all the rows. 28. Write a method that given a two dimensional array of real numbers creates and returns a one dimensional that contains sums of all the columns. 29. Write a method that given a square two dimensional array (number of columns is equal to the number of rows) prints to the screen values that appear on the diagonal of the array. 30. Write a method that given a two dimensional array returns either true, if the array has the same number of columns in each row, or false, if the array is ragged (not all rows have the same number of columns). 31. The class contract for StackOfCharacters class that we discussed is provided below. Remember that the capacity of the stack is doubled every time the size of the stack equals the capacity of the stack. Page 7

StackOfCharacters: StackOfCharacters() Constructs an empty stack of characters with a default capacity of 16. StackOfCharacters(capacity: int) Constructs an empty stack of characters with a specified capacity (if provided capacity is less than or equal to zero, the capacity is set to default 16). empty(): boolean Returns true if the stack is empty, otherwise it returns false. getsize(): int Returns the number of elements in the stack. getcapacity(): int Returns the capacity of the stack. push( value: Character ): void Puts the character value at the top of the stack. pop(): Character Removes the character from the top of the stack and returns it. If stack is empty, returns null. peek(): Character Returns the character from the top of the stack (without removing it). If stack is empty, returns null. What is the output of the following program that uses StackOfCharacters class? 1 public class TestingStack { 2 public static void main(string [] args ) { 3 StackOfCharacters s1 = new StackOfCharacters(2); 4 StackOfCharacters s2 = new StackOfCharacters(); 5 6 System.out.println( s1.getsize() ); 7 System.out.println( s2.getsize() ); 8 9 s1.push( a ); s1.push( b ); 11 s1.push( c ); 12 s2.push( w ); 13 s2.push( x ); 14 s2.push( y ); 15 16 System.out.println( s1.getsize() ); 17 System.out.println( s2.getsize() ); 18 19 System.out.println( s2.pop() ); 20 System.out.println( s2.pop() ); 21 System.out.println( s2.peek() ); 22 s2.pop(); 23 24 if (! s2.empty() ) 25 System.out.println("not empty"); 26 27 Page 8