CS2420 - Midterm Exam 1 Review

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/99

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:09 PM on 12/9/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

100 Terms

1
New cards

Which of the following is not a primitive type in Java?

A. boolean
B. double
C. short
D. String
E. none of the above

D. String

String is a reference type in Java

2
New cards

What is the type of the expression x + y in the following?

int x = 1;
double y = 3.14;
System.out.println(x + y);

A. double
B. int
C. none of the above

A. double

Mixed-type operations are not permitted.
Implicit type conversion promotes x to double.
Addition of two double types yields a double.

3
New cards

Does the following print a 3?

int choice = 2;
switch(choice) {
case 1:
System.out.print("1 ");
case 2:
System.out.print("2 ");
case 3:
System.out.print("3 ");
}
System.out.println("Done");

A. yes
B. no

A. yes

With no break statement between the sixth and seventh lines, execution continues to the third print statement.

4
New cards

After execution of the following, what is true of the value of i?

int i = 10;
myMethod(i);
System.out.println(i);

A. The value is guaranteed to be 10.
B. The value is guaranteed NOT to be 10.
C. The value may or may not be 10.

A. The value is guaranteed to be 10.

Because i has a primitive type (int), the parameter of myMethod is assigned a copy of the value 10. If myMethod makes any change, it can only be to the value of its parameter and not the value of i.

5
New cards

The following is an example of what?

public static void add(int i) {...}
public static void add(int i, int j) {...}

A. method overloading
B. method overriding
C. none of the above

A. method overloading

Within a single class, defining multiple methods with the same name and different parameter lists is called method overloading.
Method overriding can only happen in the presence of inheritance and among nonstatic methods with the same name and SAME parameter lists.

6
New cards

One can use a switch statement anywhere one can use an if-else statement.

A. true
B. false

B. false

Counterexample:
double score = ...
if(score >= 93)
System.out.println("A");
else if(score >= 90)
System.out.println("A-"); ...

7
New cards

After execution of the following, what is true of the value of arr[0]?

int[] arr = {1, 3, 5};
myMethod(arr);
System.out.println(arr[0]);

A. The value is guaranteed to be 1.
B. The value is guaranteed NOTto be 1.
C. The value may or may not be 1.

C. The value may or may not be 1.

Because arr has a reference type, the parameter of myMethod is assigned a copy of the array reference (not a copy of the actual array). Using this reference, myMethod my make a change to the same array referred to by arr.

8
New cards

Consider the following class definition.

public class MyClass {
public int i;
public String s;
}

What does MyClass c = new MyClass(); do?

A. Sets c.i to 0.
B. Sets c.s to "" (the empty string).
C. all of the above
D. none of the above

A. Sets c.i to 0.

When the author of a class does not provide a constructor, the compiler generates a default contractor that sets all member variables to 0. For a reference like s, 0 is the same as null.

9
New cards

After execution of the following, what is true of the value of str?

String str = "hi";
secretMethod(str);
System.out.println(str);

A. The value is guaranteed to be "hi".
B. The value is guaranteed not to be "hi".
C. The value may or may not be "hi".

A. The value is guaranteed to be "hi".

Even though str has a reference type, String objects are immutable. Therefore, regardless of what secretMethod does to its parameter, the value referred to by str is unchanged.

10
New cards

After execution of the following, what is true of the value returned by list.get(0)?

ArrayList list = new ArrayList();
list.add(3.14); secretMethod(list);
System.out.println(list.get(0));

A. The value is guaranteed to be 3.14.
B. The value is guaranteed NOT to be 3.14.
C. The value may or may not be 3.14.

C. The value may or may not be 3.14.

Because list has a reference type, the parameter of secretMethod is assigned a copy of the reference (not a copy of the actual object). Using this reference, secretMethod may make a change to the same object referred to by list.

11
New cards

Does the following print a c?

int choice = 6;
switch(choice) {
case -1: case 2:
System.out.print("a"); break;
case 4: case 5:
System.out.print("b");
case 10:
System.out.print("c"); break;
default:
System.out.print("d");
}

A. yes
B. no

B. no

Since 6 does not match any of the cases, the default is invoked a it is not necessary to have a break statement at the end of the switch block. What will happen for other values of choice, given the lack of a break statement between the sixth and seventh lines?

12
New cards

Sketch a diagram to illustrate the inheritance hierarchy among Shape, Rectangle, Circle, Triangle, and Square. How many levels does it have?

A. one
B. two
C. three
D. four

C. three

1 Shape
2 Circle Rectangle Triangle
3 Square

13
New cards

A derived class can ...

A derived class cannot ...

A derived class can ...
- add new fields (e.g., Rectangle can add height and width)
- add new methods (e.g., Triangle can add isEquilateral)
- override existing methods of the base class (Circle can override computeArea)

A derived class cannot ...
- remove fields
- remove methods

14
New cards

A derived class inherits all fields and methods of base.

True
False

True

15
New cards

For override, derived class method must have ______________ as base class method—otherwise, just method ______________.

same signature
overloading

16
New cards

The type known at compile time is called the ______________ type

The type known at run time is called the ______________ type.

static
dynamic

17
New cards

All reference types are polymorphic—the operation appropriate to the actual reference object will be automatically selected at ______________ time.

run (i.e., the operation appropriate to the dynamic type)

18
New cards

A class with at least ______________ abstract method is an abstract class and cannot be instantiated.

one

19
New cards

An abstract base class (or simply, abstract class) can be instantiated.

True
False

False

It cannot be instantiated—designated only as a super class

20
New cards

Behaviors defined by an abstract class are ______________.

"generic"

21
New cards

In Java, the interface is the ______________ ______________ class.

ultimate abstract

22
New cards

An interface consists only of ______________ ______________ methods and ______________ ______________ ______________ fields.

public abstract
public static final

23
New cards

How is an interface different from an abstract class?

A class may implement more than one interface, but may extend only one other class.

Note: A class may both implement interface(s) and extend another class.

24
New cards

If the class does not implement all interface methods, it is itself ______________.

abstract

25
New cards

Implementing class can be extended, and its subclasses automatically implement the same interface.

True
False

True

26
New cards

Interfaces can extend other interfaces.

True
False

True

27
New cards

Consider the following method contained in the Shape class. What is this an example of?

public boolean equals(Shape other) {
. . .
}

A. method overloading
B. method overriding

A. method overloading

The equals method inherited from Object has one parameter of type Object. Because this method has a different parameter, it is not overriding the inherited equals method. Instead, it is adding a second (overloaded) equals method.

28
New cards

Suppose that non-abstract class B is derived from abstract class A. Which of the following is prohibited?

A. A obj;
B. A obj = new A();
C. A obj = new B();
D. A contains a non-abstract method.
E. more than one of the above choices

B. A obj = new A();

Instantiation of an abstract class is not permitted. Consider the danger if it were allowed — the programmer may try to invoke an abstract method on the object, while no implementation exists to execute.

29
New cards

Consider the following class definitions:

public class Parent {
public void myMethod(int i) { ... }
}
public class Child extends Parent {
public void myMethod(int[] i) { ... }
}

Which version of myMethod is called for

Parent p = new Child(); p.myMethod(3);

A. version in Parent
B. version in Child
C. compiler error
D. run-time error

A. version in Parent

Notice that myMethod in Child does not override myMethod in Parent, due to the different parameters. Child has two myMethod methods, and the one inherited from Parent is being called (due to the int argument)

30
New cards

If implementation is identical except for the type of the object, a ______________ implementation can be used to describe basic functionality (e.g., sorting).

generic

31
New cards

Every class has Object as a superclass.

True
False

True

32
New cards

A ______________ class stores an entity and adds operations that the entity's type does not support.

wrapper

33
New cards

Java provides wrapper classes for each primitive type:

Byte for byte
Short for short
______________ for int
Long for long
Float for float
______________ for double
Boolean for boolean
______________ for char

Integer
Double
Character

34
New cards

What is auto-boxing? What is auto-unboxing?

Auto-boxing:
In Java, if an int is passed in a place where an Integer is required, the compiler will insert a call to the Integer constructor behind the scenes.

Auto-unboxing:
Similarly, if an Integer is passed where an int is required, the compiler will insert a call to the intValue method.

35
New cards

To write a generic static method, include one or more type parameters in <> just before the ______________ ______________.

return type

36
New cards

To use a generic method, you need to specify type in <>.

True
False

False

You do not need to specify the type in <>.

37
New cards

A member method in a generic class is a generic method.

True
False

False

A generic method is a (generic) static method that does not belong to a class or an object.

38
New cards

In using a generic class or method, we can be more general about the actual type to be used.

Explain <? extends SomeClass> and <? super SomeClass>.

<? extends SomeClass> allows anything that is a SomeClass (is SomeClass or derives from it).

<? super SomeClass> allows anything that is SomeClass or has a SomeClass as a superclass.

39
New cards

What is the term for an object whose sole purpose is to have its one and only method be called?

functor

40
New cards

Java does not allow functions as parameters, but we can embed a function in an object and pass a reference to it. Such an object is known as a ______________ ______________ or a ______________. It contains just one method and no data.

function object
functor

41
New cards

Consider this method:

public static void m(ArrayList a) {...}

For which of the following does m(list) compile without error?

A. ArrayList<Object> list = new ArrayList<Object>();
B. ArrayList<Shape> list = new ArrayList<Shape>();
C. ArrayList<Circle> list = new ArrayList<Circle>();
D. exactly two of the above
E. all of the above

B. ArrayList<Shape> list = new ArrayList<Shape>();

ArrayList<Shape>, ArrayList<Circle>, and any ArrayList of a type the extends Shape are type compatible with method m's parameter.

42
New cards

Consider this method:

public static void m(ArrayList<? extends Shape> a) {...}

For which of the following does m(list) compile without error?

A. ArrayList<Object> list = new ArrayList<Object>();
B. ArrayList<Shape> list = new ArrayList<Shape>();
C. ArrayList<Circle> list = new ArrayList<Circle>();
D. exactly two of the above
E. all of the above

D. exactly two of the above

ArrayList<Shape>, ArrayList<Circle>, and any ArrayList of a type the extends Shape are type compatible with method m's parameter.

43
New cards

Suppose you are developing an application that requires comparing String objects according to the sum of their characters' integer codes. Which of the following strategies should you follow?

A. Call String's existing compareTo method on the objects.
B. Rewrite the compareTo method in the String class.
C. Create a new class that implements Comparator<String>.
D. Give up — String objects can only be compared lexicographically.

C. Create a new class that implements Comparator<String>.

See today's warm-up problem, as well as Class Meeting 4's code demo, for similar examples of using Comparator in this way.

44
New cards

Sort in increasing growth rate order:

NlogN NlogN
N linear
N^2 quadratic
N^3 cubic
logN logarithmic
1 constant

1 constant
logN logarithmic
N linear
NlogN NlogN
N^2 quadratic
N^3 cubic

45
New cards

For nested loops, the running time is that of statements in the bodies ______________ ______________ the sizes of all the loops.

multiplied by

46
New cards

For a sequence of consecutive loops, the running time is that of the ______________ loop.

dominant

47
New cards

What is the growth rate?

for(int i = 0; i < n; i += 2)
sum++;

A. constant, O(1)
B. logarithmic, O(log N)
C. linear, O(N)
D. O(N log N)
E. quadratic, O(N2)
F. cubic, O(N3)

C. linear, O(N)

This (single) loop iterates N/2 times. Recall that Big-O notation does not include factors, like 1/2.

48
New cards

What is the growth rate?

for(int i = 0; i < n; i++)
for(int j = 0; j < n*n; j++)
sum++;

A. constant, O(1)
B. logarithmic, O(log N)
C. linear, O(N)
D. O(N log N)
E. quadratic, O(N2)
F. cubic, O(N3)

F. cubic, O(N3)

The outer loop iterates N times. The inner loop iterates N2 times. Recall that when loops are nested, you multiply

49
New cards

What is the growth rate?

for(int i = 1; i < n; i *= 2)
sum++;

A. constant, O(1)
B. logarithmic, O(log N)
C. linear, O(N)
D. O(N log N)
E. quadratic, O(N2)
F. cubic, O(N3)

B. logarithmic, O(log N)

This (single) loop iterates log N times — notice the "repeated doubling" behavior.

50
New cards

The ______________ bound on T(N) is a guarantee over all inputs of size N.

worst-case

51
New cards

The ______________ bound on T(N) is measured as an average over all of the possible inputs of size N.

average-case

52
New cards

Big-O is an upper bound on T(N) for ______________ or ______________.

worst-
average-case.

53
New cards

What is the Big-O behavior of binary search?

O(log N) or logarithmic behavior.

Unsuccessful search, worst-case running time of a successful search - T(N) = log N - O(log N) or logarithmic behavior

Average-case running time of a successful search - T(N) = log (N / 2) = log N - 1 - O(log N) or logarithmic behavior

54
New cards

Suppose we try to use the SimplePriorityQueue class in the way given below. What happens?

SimplePriorityQueue p = new SimplePriorityQueue((i1,i2) -> i2-i1);

A. The code does not compile.
B. A runtime exception occurs.
C. The code compiles and runs without error․

C. The code compiles and runs without error.

Like the warm-up problem, this statement creates a SimplePriorityQueue that orders items in the reverse manner of the natural ordering. The lambda expression indicates that the Comparator's compare method returns a positive value when the first integer i1 is smaller than i2 (the opposite of what Integer's compareTo method does).

55
New cards

Suppose we try to use the SimplePriorityQueue class in the way given below. What happens?

public class Point {
public int x;
public int y;
}
SimplePriorityQueue p = new SimplePriorityQueue();

A. The code does not compile.
B. A runtime exception occurs.
C. The code compiles and runs without error․

C. The code compiles and runs without error.

The constructor with no parameters is called, and it is assumed that Point is Comparable (i.e., the compareTo method will be used for comparisons).

56
New cards

Suppose we try to use the SimplePriorityQueue class in the way given below. What happens?

public class Point {
public int x;
public int y;
}
SimplePriorityQueue p = new SimplePriorityQueue();
Point a = new Point(); a.x = 3; a.y = 6;
p.insert(a);
Point b = new Point(); b.x = -1; b.y = 10;
p.insert(b);

A. The code does not compile.
B. A runtime exception occurs.
C. The code compiles and runs without error.

B. A runtime exception occurs.

An exception should happen when inserting the second Point object because it triggers a call to the inner compare method and the cast of Point to Comparable fails.

57
New cards

Can the selection sort algorithm be used for arrays/lists that contain duplicate elements?

A. Yes
B. No

A. Yes

There is nothing about the selection sort algorithm (or any sorting algorithm we will study) that would not work the same for lists with or without duplicates.

58
New cards

Which of the following do you expect gives the average-case running time of a sorting algorithm? Assume that "sorted order" is smallest to largest.

A. array/list with elements in ascending order
B. array/list with elements in descending order
C. array/list with elements in permuted order

C. array/list with elements in permuted order

59
New cards

Which of the following do you expect gives the worst-case running time of a sorting algorithm? Assume that "sorted order" is smallest to largest.

A. array/list with elements in ascending order
B. array/list with elements in descending order
C. array/list with elements in permuted order

B. array/list with elements in descending order

Also known as reverse sorted order.

60
New cards

The number of comparisons required for a selection sort will always be the same, no matter the original ordering of the items in the array/list.

A. True
B. False

A. True

Neither the outer loop nor the inner loop terminates early based on the ordering of elements.

61
New cards

Describe the selection sort algorithm.

Selection sort is one of the easiest sorting algorithms to understand (and implement).

1. Find the minimum item in the unsorted part of the array.
2. Swap it with the first item in the unsorted part.
3. Repeat steps 1 and 2 to sort the remainder of the array.

62
New cards

Describe the insertion sort algorithm.

Insertion sort is an improvement on selection sort.

1. The first array item is the sorted portion of the array
2. Take the second item and insert it in the sorted portion.
3. Repeat steps 1 and 2 to sort remainder of the array.

63
New cards

An algorithm that sorts by comparing and exchanging adjacent array items requires Ω(______________) time on average.

N2

64
New cards

Describe the Shellsort algorithm.

Shellsort is the simplest sub-quadratic sorting algorithm.

First compare array items that are far apart, then compare items that are less far apart,..., shrinking toward basic insertion sort.

- The gap sequence specifies how far apart the compared items are at each iteration.

65
New cards

The performance of Shellsort is highly dependent on the ______________ ______________.

gap sequence

also called increment sequence

66
New cards

You should use recursion even when a simple loop will do.

True
False

False

The growth rates may be the same, but recursion incurs the overhead of method calls.

67
New cards

What are the four recursion rules?

1. Always have at least one case that can be solved without using recursion.
2. Any recursive call must progress toward a base case.
3. Always assume that the recursive call works, and use this assumption to design your algorithms.
4. Never duplicate work by solving the same instance of a problem in separate recursive calls.

68
New cards

The number of comparisons required for an insertion sort will always be the same, no matter the original ordering of the items in the array/list.

A. True
B. False

B. False

The inner loop of insertion sort can "exit early" depending on the order of the items.

69
New cards

What is the growth rate of running times for insertion sort when the array/list is already sorted?

A. constant
B. logarithmic
C. linear
D. N log N
E. quadratic

C. linear

Recall that the running-time behavior of insertion sort is O(N + I), where I is the number of inversions. For a sorted list, I = 0.

70
New cards

What is the lower bound of the average behavior of any sorting algorithm that compares and exchanges adjacent array/list items?

A. logarithmic
B. linear
C. N log N
D. quadratic

D. quadratic

Recall that on average, N(N-1)/4 pairs in the array/list are inversions and must be swapped in order to sort the array/list. This represents a minimum number of operations that is quadratic, O(N2).

71
New cards

In the best case, what is the behavior of any sorting algorithm that compares and exchanges adjacent array/list items?

A. logarithmic
B. linear
C. N log N
D. quadratic

B. linear

Recall that insertion sort works by comparing and exchanging adjacent list items, and its best-case behavior is O(N)

72
New cards

What is the problem size N for
findKthLargest(List list, int k)?

A. list.size()
B. list.get(0).length + list.get(1).length + ... + list.get(list.size() - 1).length
C. list.get(0).length list.get(1).length ... * list.get(list.size() - 1).length
D. k

A. list.size()

The method's running time is dominated by the insertion sort of the list. When collecting running times, keep the length of each array small (1 to 10).

73
New cards

Describe the divide and conquer technieque.

Divide and conquer is an important problem solving technique that makes use of recursion.
- divide—smaller problems are solved recursively (except for base cases)
- conquer—solutions to the subproblems form the solution to the original problem

74
New cards

What is the MCSS running time?

T(N) = NlogN + N

75
New cards

Describe the merge sort algorithm.

Merge sort uses a divide-and-conquer strategy to sort.

1. Divide the unsorted array into two subarrays (½ size).
2. Sort each of the two subarrays (recursively).
3. Merge the two sorted subarrays into one sorted array.

76
New cards

What is the average-case behavior of merge sort?

A. logarithmic
B. linear
C. N log N
D. quadratic

C. N log N

Recall that dividing the array/list in half in each step of recursion means there are logN "levels" of recursion (to reach the base case), due to the "repeated halving". Each level of recursion invokes the merge step, which is linear in time

77
New cards

What is the worst-case behavior of merge sort?

A. logarithmic
B. linear
C. N log N
D. quadratic

C. N log N

78
New cards

What is the best-case behavior of merge sort?

A. logarithmic
B. linear
C. N log N
D. quadratic

C. N log N

In all cases, there are logN levels of recursion with linear-time merging in each. Merge sort not only has the same Big-O behavior regardless of the ordering of the input array/list, it performs exactly the same number of operations.

79
New cards

Describe the quicksort algorithm.

Quicksort also uses a divide-and-conquer strategy to sort.

1. Choose an item from the array to be the pivot.
2. Partition the array such that all items less than or equal the pivot are to the left and all items greater than are to the right.
3. Recursively sort each partition.

80
New cards

Merge sort not only has the same Big-O behavior regardless of the ordering of the input array/list, but it also performs exactly the same number of operations.

True
False

True

81
New cards

What is the lower bound of the average behavior of a sorting algorithm that compares and exchanges list items (not necessarily adjacent)?

A. logarithmic
B. linear
C. N log N
D. quadratic

C. N log N

82
New cards

What is the average-case behavior of quicksort?

A. logarithmic
B. linear
C. N log N
D. quadratic

C. N log N

83
New cards

What is the worst-case behavior of quicksort?

A. logarithmic
B. linear
C. N log N
D. quadratic

D. quadratic

84
New cards

Which of the following sorting algorithms do exactly the same amount of work (e.g., comparisons) for any array of size N, regardless of how items of the array are ordered? Select any correct answer.

A. selection sort
B. insertion sort
C. Shellsort
D. merge sort
E. quicksort

A. selection sort
D. merge sort

85
New cards

Suppose:

public class Person { ...
public class Student extends Person { ...
public class Sophomore extends Student { ...
public class CompA implements Comparator<Person> { ...
public class CompB implements Comparator<Sophomore> { ...

We need to order ArrayList<Student> list and have already written this method:

public static <T> void sort(ArrayList<T>, Comparator<? super T>)

Which of the following will work as the second argument?

A. new CompA()
B. new CompB()
C. none of the above

A. new CompA()

86
New cards

public class Car {
protected String color;
public Car() { this.color = "black";}
public int getCapacity() { return 4; }
public void setColor(String color) { this.color = color; }
public String getColor() { return color; }
}
public class Van extends Car {
public Van() {}
public int getCapacity() { return 8; }
public void setColor(int color) {
switch(color) {
case 0: this.color = "merlot";
case 1: this.color = "blue"; break;
case 2: this.color = "white";
}
}
}
public class Demo {
public static void main(String[] args) {
Van v = new Van();
System.out.println(v.getCapacity()); // What is printed?
}
}

A. 4
B. 8
C. black
D. merlot
E. blue
F. white
G. other

B. 8

87
New cards

public class Car {
protected String color;
public Car() { this.color = "black";}
public int getCapacity() { return 4; }
public void setColor(String color) { this.color = color; }
public String getColor() { return color; }
}
public class Van extends Car {
public Van() {}
public int getCapacity() { return 8; }
public void setColor(int color) {
switch(color) {
case 0: this.color = "merlot";
case 1: this.color = "blue"; break;
case 2: this.color = "white";
}
}
}
public class Demo {
public static void main(String[] args) {
Van v = new Van();
v.setColor("1");
System.out.println(v.getColor()); // What is printed?
}
}

A. 4
B. 8
C. black
D. merlot
E. blue
F. white
G. other

G. other

setColor(String color) from class Car is called, so, color will be set to "1". And "1" will be printed.

88
New cards

public class Car {
protected String color;
public Car() { this.color = "black";}
public int getCapacity() { return 4; }
public void setColor(String color) { this.color = color; }
public String getColor() { return color; }
}
public class Van extends Car {
public Van() {}
public int getCapacity() { return 8; }
public void setColor(int color) {
switch(color) {
case 0: this.color = "merlot";
case 1: this.color = "blue"; break;
case 2: this.color = "white";
}
}
}
public class Demo {
public static void main(String[] args) {
Van v = new Van();
v.setColor(0);
System.out.println(v.getColor()); // What is printed?
}
}

A. 4
B. 8
C. black
D. merlot
E. blue
F. white
G. other

E. blue

There is no break statement between cases 0 and 1.

89
New cards

public class Car {
protected String color;
public Car() { this.color = "black";}
public int getCapacity() { return 4; }
public void setColor(String color) { this.color = color; }
public String getColor() { return color; }
}
public class Van extends Car {
public Van() {}
public int getCapacity() { return 8; }
public void setColor(int color) {
switch(color) {
case 0: this.color = "merlot";
case 1: this.color = "blue"; break;
case 2: this.color = "white";
}
}
}
public class Demo {
public static void main(String[] args) {
Van v = new Van();
Object o = new Car();
flag = o.equals(o); // What is the value of flag?
}
}

A. yes
B. no

B. false

90
New cards

Consider the following snippet of Java code:

double d = 3.14;
int i = (int)d;
System.out.println("d is " + d + " and i is " + i);

Which of the following is true of executing this code?

A. When executed, it prints: d is 3 and i is 3
B. When executed, it prints: d is 3 and i is 3.14
C. When executed, it prints: d is 3.14 and i is 3
D. When executed, it prints: d is 3.14 and i is 3.14
E. The code does not compile.
F. When executed, the code raises an exception.

C. When executed, it prints: d is 3.14 and i is 3

91
New cards

Consider the following snippet of Java code:

boolean b = true;
if(10 > 100 && b)
System.out.print("1");
else
System.out.print("2");
if(10 > 100 || b)
System.out.println("3");

Which of the following is true of executing this code?

A. When executed, it prints: 1
B. When executed, it prints: 12
C. When executed, it prints: 123
D. When executed, it prints: 1
E. When executed, it prints: 2
F. When executed, it prints: 23
G. When executed, it prints: 3
H. The code does not compile.
I. When executed, the code raises an exception.

F. When executed, it prints: 23

92
New cards

Consider the following snippet of Java code:

boolean b = true;
if(10 > 100 && b)
System.out.print("1");
else
System.out.print("2");
if(10 > 100 || b)
System.out.println("3");

What value of b would cause 1 but not 3 to be printed?

A. false
B. true
C. 1
D. 3
D. none of these choices

D. none of these choices

93
New cards

Code snippet A:

for(int i = 0; i < 10; i++)for(int j = 0; j < 10; j++)
for(int k = 0; k < 10; k++)
System.out.println("Hello, World.");

Code snippet B:

for(int x = 0; x < 100; x++)
System.out.println("Hello, World.");
for(int y = 0; y < 100; y++)
System.out.println("Hello, World.");
for(int z = 0; z < 100; z++)
System.out.println("Hello, World.");

Which code snippet prints Hello, World. more times?

A. Code snippet A prints more.
B. Code snippet B prints more.
C. Both code snippets print the same amount.

A. Code snippet A prints more.

94
New cards

Consider the following Java statement:

char[] arr = new char[50];

Which of the following expressions accesses the last character of the array arr?

A. arr[50]
B. arr[last]
C. arr[arr.length - 1]
D. none of these choices

C. arr[arr.length - 1]

95
New cards

Suppose that we have defined a class called Point that contains only two public instance variables, int x and int y. Consider the following snippet of Java code:

Point p1 = null;
if(flag)
p1 = new Point();
p1.x = 3;p1.y = -5;

Point p2 = p1;
p2.x = 10;
System.out.println("p1.x is " + p1.x + " and p2.x is " + p2.x);

Suppose that the value of flag is true. Which of the following is true of executing this code?

A. When executed, it prints: p1.x is 3 and p2.x is 3
B. When executed, it prints: p1.x is 3 and p2.x is 10
C. When executed, it prints: p1.x is 10 and p2.x is 10
D. The code does not compile.
E. When executed, the code raises an exception.

C. When executed, it prints: p1.x is 10 and p2.x is 10

96
New cards

Suppose that we have defined a class called Point that contains only two public instance variables, int x and int y. Consider the following snippet of Java code:

Point p1 = null;
if(flag)
p1 = new Point();
p1.x = 3;p1.y = -5;

Point p2 = p1;
p2.x = 10;
System.out.println("p1.x is " + p1.x + " and p2.x is " + p2.x);

Suppose that the value of flag is false. Which of the following is true of executing this code?

A. When executed, it prints: p1.x is 3 and p2.x is 3
B. When executed, it prints: p1.x is 3 and p2.x is 10
C. When executed, it prints: p1.x is 10 and p2.x is 10
D. The code does not compile.
E. When executed, the code raises an exception.

E. When executed, the code raises an exception.

97
New cards

What is the testing technique in which the smallest testable parts of an application are individually and independently tested for correctness called?

A. integrated testing
B. regression testing
C. test first
D. unit testing
E. none of these choices

D. unit testing

98
New cards

Which of the following is true regarding Java interfaces? (Select all that apply.)

A. All methods that are members of an interface are abstract.
B. All members of an interface (methods and data) are public.
C. A class that implements an interface must not perform any method overloading.
D. All methods that are members of an interface must have a return type of void.
E. All abstract classes are interfaces.

A. All methods that are members of an interface are abstract.
B. All members of an interface (methods and data) are public.

99
New cards

class Person { public String getAddress() ... }
class Employee extends Person { public double getSalary() ... }
class Student extends Person { public String toString() ... }
class GradStudent extends Student { public boolean equals(Object other) ... }

Employee e = new Employee();
Student s = new GradStudent();
Person p = e;
Object o = s;

What is true of the method call p.getSalary()?

A. At runtime, an exception is thrown.
B. It does not compile.
C. It invokes the getSalary method from the Employee class.

B. It does not compile.

100
New cards

class Person { public String getAddress() ... }
class Employee extends Person { public double getSalary() ... }
class Student extends Person { public String toString() ... }
class GradStudent extends Student { public boolean equals(Object other) ... }

Employee e = new Employee();
Student s = new GradStudent();
Person p = e;
Object o = s;

What is true of the method call s.toString()?

A. At runtime, an exception is thrown.
B. It invokes the toString method from the Student class.
C. It does not compile.
D. It invokes the toString method from the Object class.

B. It invokes the toString method from the Student class.