Sem 1 Practice Final 2-2016
Sem 1 Practice Final 2-2016
(A) Hoho
(B) HOho
(C) HoHo
(D) hoHO
(E) falalalala-lalalala
public class A
{
public String toString ( )
{
return "A";
}
}
}
}
A a = new C();
System.out.println(a); // note that this will invoke a toString() method
(A) A
(B) C
(C) AC
(D) ACDC
(E) ABCD
1
3. Consider the following code segment.
(A) [2, 1, 0]
(B) [2, 1, 0, 1, 2]
(C) [2, 1, 0, 2, 1]
(D) [2, 1, 2, 1, 0]
(E) [2, 2, 2, 2, 1, 0]
4. What values are stored in the array arr aKer the following code is executed?
(A) 0, 0, 0, 0, 0
(B) 1, 2, 3, 4, 5
(C) 0, 1, 3, 6, 10
(D) 1, 3, 6, 10, 15
(E) 1, 4, 10, 20, 35
5. Suppose x is an int variable that holds a posiQve integer. Consider the following five expressions:
a. x % 100 / 10
b. x / 10 % 10
c. (x - x % 100) / 10
d. x / 10 - x / 100 * 10
e. (x - x / 100 * 100) / 10
Which one of them produces a value different from the other four for some values of x?
(A) a.
(B) b.
(C) c.
(D) d.
(E) e.
2
6. The relaQonship between a child (sub) class and a parent (super) class is referred to as a __________ relaQonship.
(A) has-a
(B) was-a
(C) instance-of
(D) is-a
(E) whatever-a
7. Suppose an interface Polygon specifies the getArea() method. Two classes, Triangle and Hexagon, implement
Polygon. Which Java feature makes it possible for the following code segment to print the correct values for the area of
a triangle and a hexagon?
(A) abstracQon
(B) polymorphism
(C) encapsulaQon
(D) plaZorm independence
(E) method overloading
// precondiFon: x >= 0
public void mystery (int x)
{
if ((x / 10) != 0)
mystery(x / 10);
System.out.print(x % 10);
}
(A) 16
(B) 56
(C) 123456
(D) 654321
(E) Many digits are printed due to infinite recursion.
9. The class CourseList provides methods that allow you to represent and manipulate a list of high school courses, but
you are not concerned with how these operaQons work or how the list is stored in memory. You only know how to
iniQalize and use CourseList objects and have no direct access to the implementaQon of the CourseList class or its
private data fields. This is an example of
(A) encapsulaQon
(B) overriding
(C) inheritance
(D) polymorphism
(E) method overloading
3
10. Which of the following statements is equivalent to
harry.increase(20);
4
12. Consider the following method:
return count;
}
(A) 21
(B) 28
(C) 42
(D) 49
(E) 343
(A) A class that can be hard to interpret, like the modern art of Picasso
(B) A class in which all fields and methods are staQc
(C) A class where some methods are abstract
(D) A class where all methods are abstract
(E) Any class derived from the Object class
5
For quesQons 15 – 17, use the following parQal class definiQons:
15. Which of the following lists of instance data are accessible in class Daughter?
16. Which of the following lists of instance data are accessible in class Dad?
17. Which of the following is true regarding the use of instance data changedish of class Dad?
6
18. Assume that an array of double values has been declared as follows and has been iniQalized.
Which of the following code segments correctly interchanges the value of arr[0] and arr[4]?
(A) I only
(B) III only
(C) I and III only
(D) None of these
(E) I, II, and III
20. Which of the following indicates that a method does not take any parameters?
7
21. Assume that the following variable declaraQons have been made.
double d = Math.random();
double r;
Which of the following assigns a value to r from the uniform distribuQon over the range 0.5 <= r < 5.5 ?
(A) r = d + 0.5;
(B) r = d + 0.5 * 5.0;
(C) r = d + 5.0;
(D) r = d * 5.5;
(E) r = d * 5.0 + 0.5;
What values are stored in myints aKer the following code is executed?
(A) 0, 0, 0, 0
(B) 1, 1, 1, 1
(C) 4, 3, 2, 1
(D) 4, 0, 0, 0
(E) 1, 2, 3, 4
23. Why doesn’t Java let you create an object of the Math class?
8
24. Consider the following method, isSorted, which is intended to return true if an array of integers is sorted in
nondecreasing order and to return false otherwise.
Which of the following can be used to replace /* missing code */ so that isSorted will work as intended?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I and III only
25. Which of the following best describes the base case(s) in the following recursive method?
9
26. Which of the following is a good stylisQc rule for naming fields?
int k = 0;
while (k < 10)
{
System.out.print((k % 3) + " ");
if ((k % 3) == 0)
k = k + 2;
else
k++;
}
(A) 0 2 1 0 2
(B) 0 2 0 2 0 2
(C) 0 2 1 0 2 1 0
(D) 0 2 0 2 0 2 0
(E) 0 1 2 1 2 1 2
10
30. Consider the following code segment.
int a = 24;
int b = 30;
while (b != 0)
{
int r = a % b;
a = b;
b = r;
}
System.out.println(a);
(A) 0
(B) 6
(C) 12
(D) 24
(E) 30
(A) "CMUE"
(B) "OPTR"
(C) "OMPUTER"
(D) "COMPUTE"
(E) "COMPUTER"
11
33. Consider the following code segment.
double a = 1.1;
double b = 1.2;
if ((a + b) * (a - b) != (a * a) - (b * b))
{
System.out.println("MathemaFcal error!");
}
Which of the following best describes why the phrase "MathemaFcal error!" would be printed?
(Remember that mathemaQcally (a+b)*(a-b) = a2 - b2 .)
Which of the following represents the contents of array as a result of execuQng the code segment?
(A) 2, 4, 6, 8, 10, 12
(B) 2, 6, 6, 8, 10, 12
(C) 2, 4, 8, 8, 10, 12
(D) 2, 4, 8, 10, 12, 12
(E) 2, 6, 8, 10, 12, 12
(A) 2 bytes
(B) 4 bytes
(C) 8 bytes
(D) 16 bytes
(E) It depends on the operaQng system
(A) 3
(B) 4
(C) 5
(D) 8
(E) As many as the programmer defines.
12
37. All classes in Java are directly or indirectly subclasses of the ____________ class.
(A) Wrapper
(B) String
(C) Reference
(D) this
(E) Object
}
}
}
}
Now, consider the following declaraQons in a client class. You may assume that ClassOne and ClassTwo have default
constructors.
I. c1.methodTwo();
II. c2.methodTwo();
III. c2.methodOne();
13
39. What happens if str.length() is 8 and you call str.charAt(8) ?
14
41. Which of the following can replace LINE 1 in the MovieWithRaFng constructor, so that
will print
Breaking Dawn
(A) Ftle = t; (C) super.setTitle(t); (E) None of the above will work
(B) setTitle(t); (D) both (B) and (C) will work
to display
The Muppets, PG
15
43. Suppose numbers is an ArrayList<Integer>, and System.out.println(numbers) displays
int sum = 0;
int k = 1;
System.out.println(sum);
(A) 6
(B) 10
(C) 12
(D) 15
(E) Nothing is printed due to an infinite loop.
45. Assume that myList is an ArrayList that has been correctly constructed and populated with objects. Which of the
following expressions produces a valid random index for myList?
A. (int)(Math.random() * myList.size()) - 1
B. (int)(Math.random() * myList.size())
C. (int)(Math.random() * myList.size()) + 1
D. (int)(Math.random() * (myList.size() + 1))
E. Math.random(myList.size())
16