Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Object Oriented Programming MCQs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

CSAP Review – 100 Sample Multiple Choice Questions

1. Object Oriented Programming

(A) makes programs more reliable.


(B) simulates real life.
(C) uses a lot of intimidating vocabulary, which is not as bad as it sounds.
(D) is all of the above.

2. The actions in a Java class are called

(A) procedures.
(B) functions.
(C) methods.
(D) subroutines.

3. Which of the following Math class features do not use any parameters or
arguments?

(A) PI
(B) E
(C) final
(D) Both A and B

4. What is the value of result in the following statement?

int result = Math.pow(3,4);

(A) 12
(B) 81
(C) 64
(D) 4

For questions 5-8 use the following Bank class information. It contains the
headings of the methods in the Bank class, along with a description.
public Bank()
// default constructor starts checking and savings account with zero
dollars.

public Bank(double c, double s)


// parameter creates an object with c dollars in checking and s dollars in
savings.
public double getChecking()
// returns the checking account balance

public double getSavings()


// returns the savings account balance

public double getCombined()


// returns the combined balance of the checking and savings account

public void changeChecking(double amount)


// alters the balance of the checking account by the amount parameter

public void changeSavings(double amount)


// alters the balance of the savings account by the amount parameter

public void closeChecking()


// alters the checking account balance to zero

public void closeSavings()


// alters the savings account balance to zero

5. The methods in the Math class are

(A) class methods.


(B) object methods.
(C) expression methods.
(D) variable methods.

6. The methods in the Bank class are

(A) class methods.


(B) object methods.
(C) expression methods.
(D) variable methods.

7. Access to methods of the Bank class requires


(A) using a statement, like Bank.getSavings();
(B) using a statement, like Bank.getSavings;
(C) the creation of one or more Bank objects.
(D) using the get method.

8. What is the output of the following program segment?

Bank tom;
tom = new Bank();
Bank sue;
sue = new Bank();
tom.changeChecking(1000);
sue.changeChecking(1500);
System.out.println("sue: " + sue.getSavings());
System.out.println("tom: " + tom.getSavings());

(A) tom: 1000.0


sue: 1500.0

(B) sue: 1500.0


tom: 1000.0

(C) sue: 0.0


tom: 0.0

(D) Error message

9. Assume that rand is an object of the Random class. Which of the following
statements generates a random integer in the [400..1600] range?

(A) int number = rand.nextInt(1600) + 400;


(B) int number = rand.nextInt(1200) + 400;
(C) int number = rand.nextInt(1600);
(D) int number = rand.nextInt(1201) + 400;

10. The kind of output created by an object of the DecimalFormat class is


determined by

(A) the type of parameter used with the construction of a new DecimalFormat
object.
(B) using the format method.
(C) using the output method.
(D) all of the above.

11. A conditional statement is


(A) a program expression that evaluates to true or false.
(B) a program statement that requires input from the keyboard.
(C) any program statement with a binary operator.
(D) any program statement with an unary operator.

12. What is the output of the following program segment?

double bonus = 500.0;


double sales = 200000.0;
if (sales >= 300000.0)
bonus += 250.0;
System.out.println("Bonus: " + bonus);
System.out.println("The End");

(A) Bonus: 50.0


The End
(B) Bonus: 500.0
The End
(C) Bonus: 750.0
The End
(D) No output

13. What is the output of the following program segment?

double bonus = 500.0;


double sales = 200000.0;
if (sales >= 300000.0)
bonus += 250.0;
System.out.println("Bonus: " + bonus);
System.out.println("The End");

(A) Bonus: 50.0


The End
(B) Bonus: 500.0
The End
(C) Bonus: 750.0
The End
(D) No output

14. What is the output of the following program segment?

double bonus = 500.0;


double sales = 200000.0;
if (sales >= 300000.0)
System.out.println("Bonus: " + bonus);
bonus += 250.0;

(A) Bonus: 250.0


(B) Bonus: 500.0
(C) Bonus: 750.0
(D) No output
15. What is the output of the following program segment?

int n1 = 100;
int n2 = 200;
int n3 = n1 / n2;
if (n3 > 0)
{
n2 = n1;
n1 = n2;
}
else
{
n1 = n2;
n2 = n1;
}
System.out.println(n1 + " " + n2);

(A) 100 200


(B) 200 100
(C) 200 200
(D) 100 100

16. What is the value of num at the conclusion of the following program segment?

char qwerty = 'B';


int num = 100;
switch(qwerty)
{
case 'A':
num ++;
case 'B':
num += 2;
case 'C':
num += 3;
case 'D':
num += 4;
}
(A) 100
(B) 102
(C) 109
(D) Error message

17. What is the value of num at the conclusion of the following program segment?

char qwerty = 'B';


int num = 100;
switch(qwerty)
{
case 'A':
num ++;
break;
case 'B':
num += 2;
break;
case 'C':
num += 3;
break;
case 'D':
num += 4;
}

(A) 100
(B) 101
(C) 102
(D) Error message

18. What is the output of the following program segment?

int count = 1;
for (int k = 0; k < 100; k++)
count++;
System.out.println(count);

(A) 99
(B) 100
(C) 101
(D) 102

19. What is the output of the following program segment?


int count1 = 1;
int count2 = 2;
for (int k = 0; k <= 5; k++)
{
count1++;
count2++;
}
System.out.println(count1 + " " + count2);

(A) 6 7
(B) 7 8
(C) 6 3
(D) 7 3

20. What is the output of the following program segment?

int sum = 0;
for (int k = 1; k < 9; k+=2)
sum += k;
System.out.println("sum: " + sum);

(A) sum: 8
(B) sum: 9
(C) sum: 16
(D) sum: 25

21. What is the output of the following program segment?

int num1 = 120;


int num2 = 108;
int num3 = 0;
do
{
num3 = num1 % num2;
if (num3 == 0)
System.out.println(num2);
else
{
num1 = num2;
num2 = num3;
}
}
while (num3 != 0);

(A) 0
(B) 6
(C) 12
(D) 36

22. A class method call

(A) requires that the class identifier precedes the method identifier.
(B) may be called with the method identifier only in certain circumstances.
(C) is only possible after a new object is constructed.
(D) uses the class identifier only for readability.

23. What is the output of the following program?

public class Q39


{
public static void main(String args [ ])
{
method1();
method3();
method2();
}

public static void method1()


{
System.out.println("Calling method 1");
}

public static void method2()


{
System.out.println("Calling method 3");
}

public static void method3()


{
System.out.println("Calling method 2");
}

(A) Calling method 1


Calling method 2
Calling method 3

(B) Calling method 1


Calling method 3
Calling method 2
(C) method 1
method 3
method 2

(D) Error message

24. What is the output of the following program?

public class Q41


{
public static void main(String args [ ])
{
method1(1);
method3(2);
method2(3);
}

public static void method1(int n)


{
System.out.println("Calling method " + n);
}

public static void method2(int n)


{
System.out.println("Calling method " + n);
}

public static void method3(int n)


{
System.out.println("Calling method " + n);
}

(A) Calling method 1


Calling method 2
Calling method 3

(B) Calling method 1


Calling method 3
Calling method 2
(C) method 1
method 3
method 2

(D) Error message

25. What is the output of the following program?

public class Q42


{
public static void main(String args [ ])
{
Q42.method1(int n = 1 );
Q42.method2(int n = 2);
Q42.method3(int n = 3);
}

public static void method1(int n)


{
System.out.println("Calling method " + n);
}

public static void method2(int n)


{
System.out.println("Calling method " + n);
}

public static void method3(int n)


{
System.out.println("Calling method " + n);
}
}

(A) Calling method1


Calling method2
Calling method3

(B) Calling method1


Calling method3
Calling method2

(C) method1
method2
method3

(D) Error message

26. What is the output of the following program?

public class Q44


{
public static void main(String args [ ])
{
int x = 25;
int y = 10;
System.out.println(Calc.add(x,y));
System.out.println(Calc.sub(x,y));
System.out.println(Calc.mul(x,y));
System.out.println(Calc.div(x,y));
}
}

class Calc
{
public static void add(int p, int q)
{
int result = p + q;
System.out.println(p + " + " + q + " = " + result);
}
public static void sub(int p, int q)
{
int result = p - q;
System.out.println(p + " - " + q + " = " + result);
}
public static void mul(int p, int q)
{
int result = p * q;
System.out.println(p + " * " + q + " = " + result);
}
public static void div(int p, int q)
{
int result = p / q;
System.out.println(p + " / " + q + " = " + result);
}
}

(A) 25 + 10 (B) 25 + 10 = 35 (C) 35


25 - 10 25 - 10 = 15 15
25 * 10 25 * 10 = 250 250
26 / 10 25 / 10 = 2 2

(D) Error message

27. Class methods are typically used when

(A) only a single copy of the class needs to be loaded


(B) multiple copies or instances of a class are required.
(C) it is not necessary to pass information to the methods.
(D) only return methods are used in a class.

28. Object methods are typically used when

(A) only a single copy of the class needs to be loaded


(B) multiple copies or instances of a class are required.
(C) it is not necessary to pass information to the methods.
(D) only return methods are used in a class.

29. Which of the following statements shows correct syntax to create an object of the
Piggy class?

(A) Piggy new tom = Piggy();


(B) Piggy = new tom();
(C) Piggy tom = new Piggy();
(D) tom = new Piggy;

30. An object is

(A) one instance of a class.


(B) another word for a class.
(C) a class with static methods.
(D) a method that accesses class attributes.

31. Calling a class method requires using

(A) a class identifier followed by a dot and a method identifier.


(B) a method identifier followed by a dot and a class identifier.
(C) an object identifier followed by a dot and a method identifier.
(D) a method identifier followed by a dot and an object identifier.

32. Calling an object method requires using

(A) a class identifier followed by a dot and a method identifier.


(B) a method identifier followed by a dot and a class identifier.
(C) an object identifier followed by a dot and a method identifier.
(D) a method identifier followed by a dot and an object identifier.
33. When is a constructor called?

(A) Each time the constructor identifier is used in a program statement


(B) During the instantiation of a new object
(C) During the construction of a new class
(D) At the beginning of any program execution

34. What is an overloaded constructor?

(A) A constructor with too many program statements.


(B) A second constructor with the same constructor heading as the first
constructor.
(C) A second constructor with a different identifier than the first constructor.
(D) A second or other multiple constructor with a different signature than any
other constructor.

35. Access to private data or private methods is

(A) restricted to methods of the same class.


(B) restricted to methods of other classes.
(C) available to methods of the same class and other classes.
(D) not an issue because the program will not compile.

36. A default constructor

(A) is a no-parameter method, which is called automatically during the


instantiation of a new object.
(B) is a parameter method, which is called automatically during the
instantiation of a new object.
(C) is a no-parameter method.
(D) is a parameter method.

37. A parameter constructor

(A) is a no-parameter method, which is called automatically during the


instantiation of a new object.
(B) is a parameter method, which is called automatically during the
instantiation of a new object.
(C) is a no-parameter method.
(D) is a parameter method.

38. Inheritance is the process of

(A) using classes in the established standard Java Language library.


(B) using features from an existing class.
(C) combining data and the methods, which process the data, inside the same
module.
(D) dividing a program into multiple related files for each class in the
program.

39. A class, which can use all the features of an established class, is

(A) a static class.


(B) a superclass.
(C) a subclass.
(D) overloaded.

40. An established class, whose members can all be used by a newly declared class, is

(A) a static class.


(B) a superclass.
(C) a subclass.
(D) overloaded.

41. Which identifier shows up both in the superclass and the subclass?

(A) The superclass identifier


(B) The subclass identifier
(C) The class identifier containing the main method
(D) The constructor identifier

42. A subclass has access to the data attributes of a superclass

(A) if the superclass data is declared protected.


(B) if the superclass data is declared private or protected.
(C) in all cases due to inheritance.
(D) only if the primary program class is declared public.

43. How is information passed from the subclass constructor to the superclass
constructor?

(A) The superclass constructor is automatically called before the subclass


constructor.
(B) Use the super keyword followed by a parameter list for the superclass.
(C) Use the super keyword followed by the superclass identifier.
(D) Use the new operator inside the subclass constructor to instantiate the
superclass.

44.. If a subclass has the same method identifier as a superclass method,

(A) a compile error message will result.


(B) a logic error will result.
(C) the superclass method will be used.
(D) the subclass method will be used.

45. Which of the following columns is the correct truth table for (A or B) and B ?

A B (A) (B) (C) (D) (E)


T T T T T F F
T F T F T F T
F T T T F T F
F F T F F T T

46. Which of the following columns is the correct truth table for (A or B) or A ?

A B (A) (B) (C) (D) (E)


T T T T T F T
T F T F F F T
F T T T F T T
F F T F F T F

47. The Boolean expression A >= B is equivalent to which of the following


expressions?

(A) not (A > B)


(B) not (B >= A)
(C) not (A < B)
(D) A != B
(E) A <= B

48. The Boolean expression A > B is equivalent to which of the following


expressions?

(A) not (B > A)


(B) not (A <= B)
(C) not (A != B)
(D) B>A
(E) B >= A

49. The Boolean expression (A and B) or A is true

(A) only when A is true.


(B) only when B is true.
(C) whenever either A is true or B is true.
(D) only whenever both A is true and B is true.
(E) for all values of A and B.

50. The Boolean expression (A && B) && !(A && B) evaluates to


(A) false in all cases.
(B) true in all cases.
(C) true whenever only A is true or only B is true.
(D) true whenever either A is true or B is true.
(E) true whenever both A is true and B is true.

51. The for loop is ideal for loop structures that

(A) repeat some process a fixed number of times.


(B) must execute some process at least one time.
(C) must check the loop condition before the loop body is executed.
(D) do all of the above.

52. The do..while loop is ideal for loop structures that

(A) repeat some process a fixed number of times.


(B) must execute some process at least one time.
(C) must check the loop condition before the loop body is executed.
(D) do all of the above.

53. What is the output of the program segment below?

int a, b;
a = some mystery int.
if (a > 100)
{
if (a < 50)
b = 1000;
else
b = 3000;
}
else
{
if (a > 150)
b = 2000;
else
b = 3000;
}
System.out.print(b);

(A) 1000

(B) 2000
(C) 3000

(D) The value of b cannot be determined without knowing the value of a.

54. What is the last output by the following program segment?

int x, y;
x = 1;
while (x < 3)
{
y = 1;
x++;
while (y < 3)
{
y++;
System.out.println(x + y);
}
}

(A) 4
(B) 5
(C) 6
(D) 7

55. How many ampersands (&) are displayed by the program segment below?

int n, p, q;
n = 5;
p = 1;
do
{
q = p;
while (q < n)
{
q += 2;
System.out.print("&");
}
p += 2;
}
while ( p < n);
(A) 1
(B) 2
(C) 3
(D) 5

56. Assume that you are writing a program, which will enter grades and average
them. You want this program to prevent the user from entering any grades that
are outside the range of [0..100]. Which of the following program segments will
only accept the desired input?

I. int grade
do
{
( prompt and get user input for grade )
}
while (grade < 0 || grade > 100);

II. int grade


do
{
( prompt and get user input for grade )
}
while ( !(grade < 0) || !(grade > 100) );

III. int grade


do
{
( prompt and get user input for grade )
}
while ( !(grade >= 0 && grade <= 100) );

(A) I only
(B) II only
(C) I & II only
(D) II and III only
(E) I and III only

57. Assume that you are executing a program segment, which repeats until user input
states to stop.
The user is prompted in some manner to enter 'Y', 'y', 'N' or 'n', where Y means
to repeat the
program segment and N means to stop execution. Which of the following
program segments will
only accept the desired input

I. char choice;
boolean ok;
do
{
( prompt user and get input for choice )
ok = (choice == 'Y' || choice == 'y' || choice == 'N' ||
choice == 'n');
}
while (!ok);

II. char choice;


boolean notOK;
do
{
( prompt user and get input for choice )
notOK = (choice != 'Y' && choice != 'y' && choice !=
'N' && choice != 'n');
}
while (notOK);

III. char choice;


boolean notOK;
do
{
( prompt user and get input for choice )
notOK = (choice != 'Y' || choice != 'y' || choice != 'N' ||
choice != 'n');
}
while (notOK);

(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II and III

58. Computer colors are created with a combination of


____________________________ values.
(A) red, green and blue
(B) red, yellow and blue
(C) blue, green and yellow
(D) blue, green and white

59. An array is a

(A) data structure with one, or more, elements of the same type.
(B) data structure with LIFO access.
(C) data structure, which allows transfer between internal and external storage.
(D) data structure with one, or more, elements, called fields, of the same or
different data types.

60. Consider the program segment below.

int list[];
list = new int[100];

How many integers can be stored in the list array

(A) 99
(B) 100
(C) 101
(D) 100 initial integers plus any additional integers required during program
execution

61. Consider the two program segments below.

Segment1 Segment2

int list[]; list[] = new int[100];


list = new int[100];

Which of the following is a true statement about the comparison of Segment1 and
Segment2?

(A) Segment1 declares list correctly. Segment2 declares list incorrectly.


(B) Segment1 declares list incorrectly. Segment2 declares list correctly.
(C) Both Segment1 and Segment2 declare list correctly.
(D) Both Segment1 and Segment2 declare list incorrectly.

62. What is the output of program Java1314.java below.

public class Java1314


{
public static void main(String args[])
{
int list[] = {1,2,3,4,5};
for (int k = 1; k < list.length; k++)
System.out.println("list[" + k + "] = " + list[k]);
}
}

(A) list[0] = 0
list[1] = 1
list[2] = 2
list[3] = 3
list[4] = 4

(B) list[0] = 1
list[1] = 2
list[2] = 3
list[3] = 4
list[4] = 5

(C) list[1] = 1
list[2] = 2
list[3] = 3
list[4] = 4
list[5] = 5

(D) list[1] = 2
list[2] = 3
list[3] = 4
list[4] = 5

(E) Compile Error

63. What is the output of the program below?

public class Java1326


{
public static void main(String args[])
{
int matrix[ ][ ];
matrix = new int[3][4];
for(int p = 0; p < 3; p++)
{
for(int q = 0; q < 4; q++)
System.out.print(matrix[p][q] + " ");
System.out.println();
}
System.out.println();
}
}

(A) 0 0 0 0 (B) 0 0 0 (C) 0


0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0

(D) Compile Error

64. What is the output of the program below?

public class Java1327


{
public static void main(String args[])
{
int matrix[ ][ ];
matrix = new int[3][4];
for(int p = 0; p < 3; p++)
{
for(int q = 0; q < 4; q++)
System.out.print(matrix[q][p] + " ");
System.out.println();
}
System.out.println();
}
}

(A) 0 0 0 0 (B) 0 0 0 (C) 0


0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0

(D) Error message

65. __________ is the principle of breaking up a program into smaller, manageable


program modules.
(A) Encapsulation
(B) Inheritance
(C) Polymorphism
(D) Divide and conquer

66. A class

(A) is a user-defined data type.


(B) combines both data and the methods that act upon the data in the same
module.
(C) is one instance of a more general data type.
(D) is both A and B.

67. An object

(A) is a user-defined data type.


(B) combines both data and the methods that act upon the data.
(C) is one instance of a more general data type.
(D) is both A and B.

68. The data in a class are also called

(A) attributes.
(B) instance variables.
(C) fields.
(D) all of the above.

69. Methods are

(A) action modules that process data.


(B) class variables that store information.
(C) instances of a class.
(D) none of the above.

70. Instantiation is the moment that

(A) memory is allocated for a specific object of a class.


(B) memory is allocated for a specific object, which is a member of a class.
(C) a program is ready for execution.
(D) a program compiles correctly.

71. Object Oriented Programming is characterized by using

(A) encapsulation.
(B) inheritance.
(C) polymorphism.
(D) all of the above.

72. A constructor is

(A) a method with the same identifier as the class identifier.


(B) neither a void method nor a return method.
(C) called during the instantiation of a new object.
(D) all of the above.

73. The scope of an object is the

(A) size of the memory allocated for an object.


(B) total number of data attributes used by an object.
(C) range of accessing member methods.
(D) period during which an object is defined and allocates memory to store
values.

74. When simple data types are used for parameter passing,

(A) the actual current value of the simple data type's variable is copied.
(B) the initial value of the simple data type's variable is copied.
(C) the memory reference where the variable values are stored is copied.
(D) a new object of the data type is instantiated.

75. When objects are used for parameter passing,

(A) the current values of the data attributes of the object are copied.
(B) the initial values of the object instantiation information are copied.
(C) the memory reference where the object information is stored is copied.
(D) a new object of the same class as the parameter object is instantiated.

76. Information hiding is the concept of

(A) declaring all significant data as private.


(B) storing information in private data fields.
(C) thinking about programming features without concern about the
implementation of these features.
(D) testing a program without using any actual data.

77. The creation of a String object

(A) requires the new operator.


(B) can be done with or without the new operator.
(C) is always done without the new operator.
(D) requires using the new operator as well as one String parameter.
78. Which of the following declarations is correct?

I. String s1 = "Mambo";

II. String s2;


s2 = "Mambo";

III. String s3 = new String("Mambo");

(A) I only
(B) I and II only
(C) II and III only
(D) I, II and III

79. What information will be stored in string3 as a result of the statement below?

String string3 = "100" + "200";

(A) The String value "100200"


(B) The int value 100200
(C) The int value 300
(D) The String value "300"

80. Assume that s1, s2 and s3 are String objects. Which statement(s) demonstrate(s)
string concatenation?

(A) s1 = s2.concat("Hello");
(B) s3 = s1 + s2;
(C) concat(s1,s2);
(D) Both A and B

81. Consider the linearSearch method below.

public boolean linearSearch(int searchNumber)


{
boolean found = false;
for (int k = 0; k < intArray.length; k++)
if (intArray[k] == searchNumber)
found = true;
return found;
}

Why is this implementation of the Linear Search algorithm is considered


undesirable?
(A) Searching will continue after a match for searchNumber is found.
(B) It will only return the correct result if the last array element is the
searchNumber.
(C) It will only return the correct result if the first array element is the
searchNumber.
(D) It will return the wrong result if the searchNumber is not found.

82. Consider method whatSort below.

public void whatSort()


{
for (int q = 0; q < intArray.length-1; q++)
if ( intArray[q] > intArray[q+1])
{
int temp = intArray[q];
intArray[q] = intArray[q+1];
intArray[q+1] = temp;
}
}

Assume that intArray stores a random list of integers prior to calling whatSort.
How will the
Integers be arranged after a call to whatSort?

(A) Data will be arranged in ascending order.


(B) Data will be arranged in descending order.
(C) The largest integer will be stored in the last array element.
(D) The smallest integer will be stored in the first array element.

83. Consider method whatSort below.

public void whatSort()


{
for (int p = 1; p < intArray.length; p++)
for (int q = 0; q < intArray.length-p; q++)
if ( intArray[q] > intArray[q+1])
{
int temp = intArray[q];
intArray[q] = intArray[q+1];
intArray[q+1] = temp;
}
}

Assume that intArray stores a random list of integers prior to calling whatSort.
How will the
Integers be arranged after a call to whatSort?
(A) Data will be arranged in ascending order.
(B) Data will be arranged in descending order.
(C) The largest integer will be stored in last array element.
(D) The smallest integer will be stored in the first array element.

84. Assume that index represents the array location where a new array elements needs
to be deleted or an
existing array element needs to be deleted. Variable size represents the number of
elements
in intArray. Consider method whichOne below.

public void whichOne(int index, int number)


{
for (int k = size-1; k > index; k--)
intArray[k] = intArray[k-1];
intArray[index] = number;
}

Method whichOne

(A) deletes number from the array.


(B) inserts number into the array.
(C) either deletes number or inserts number; it depends on how method
whichOne is called.
(D) either deletes number or inserts number; it depends on the value of index.

85. Assume that index represents the array location where a new array elements needs
to be deleted or an
existing array element needs to be deleted. Variable size represents the number of
elements
in intArray. Consider method whichOne below.

public void whichOne(int index, int number)


{
for (int k = index; k < size-1; k++)
intArray[k] = intArray[k+1];
size--;
}

Method whichOne

(A) deletes number from the array.


(B) inserts number into the array.
(C) either deletes number or inserts number; it depends on how whichOne is
called.
(D) either deletes number or inserts number; it depends on the value of index.

86. How does the Selection Sort compare to the Bubble Sort in execution efficiency?

(A) The Selection Sort is always faster than the Bubble Sort.
(B) The Bubble Sort is always faster than the Selection Sort.
(C) The Selection Sort is usually faster than the Bubble Sort with random
data.
(D) The Selection Sort is only faster than the Bubble Sort with sorted data.

87. Consider method selectionSort below. Assume that method swap exists, which
swaps the array
elements of intArray according to the provided index parameters.

public void selectionSort()


{
for (int p = 0; p < intArray.length-1; p++)
{
int temp = p;
for (int q = p+1; q < intArray.length; q++)
if (intArray[q] < intArray[temp])
temp = q;
if (intArray[p] != intArray[temp])
swap(p,temp);
}
}

Assume that intArray stores a random list of integers prior to calling


selectionSort. How will the
Integers be arranged after a call to selectionSort?

(A) Data will be arranged in ascending order.


(B) Data will be arranged in descending order.
(C) The largest integer will be stored in last array element.
(D) The smallest integer will be stored in the first array element.

88. Consider method selectionSort below. Assume that method swap exists, which
swaps the array
elements of intArray according to the provided index parameters.

public void selectionSort()


{
for (int p = 0; p < intArray.length-1; p++)
{
int temp = p;
for (int q = p+1; q < intArray.length; q++)
if (intArray[q] > intArray[temp])
temp = q;
if (intArray[p] != intArray[temp])
swap(p,temp);
}
}

Assume that intArray stores a random list of integers prior to calling


selectionSort. How will the
Integers be arranged after a call to selectionSort?

(A) Data will be arranged in ascending order.


(B) Data will be arranged in descending order.
(C) The largest integer will be stored in last array element.
(D) The smallest integer will be stored in the first array element.

89. How does the Selection Sort behave after all the data is sorted in an array?

(A) It stops making comparisons, just like the Smart Bubble Sort.
(B) The Regular Selection Sort continues sorting; the Smart Selection Sort
stops.
(C) The Selection Sort continues to make comparisons until every comparison
pass is finished.
(D) If the data is sorted already, the Selection Sort never starts, otherwise it
executes every loop.

90. Is the Binary Search always preferred over the Linear Search, and why?

(A) Yes, because the Binary Search is always faster than the Linear Search.
(B) Yes, because the Binary Search can search any type of data.
(C) No, because the Linear Search is faster than the Binary Search with sorted
data.
(D) No, because the Binary Search only works with sorted data, unlike the
Linear Search.

91. Assume that intArray contains the following set of sorted integers.

{100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200}

How many comparisons are made by a Binary Search method to find number
130?

(A) 2
(B) 3
(C) 4
(D) 5
92. Assume that intArray contains the following set of sorted integers.

{11,22,33,44,55,66,77,88,99}

How many comparisons are made by a Binary Search method to determine that
number 50
is not in the list?

(A) 2
(B) 3
(C) 4
(D) 5

93. Which of the following statements is a/are fundamental recursion concept(s)?

(A) All recursive methods require an exit or base case.


(B) Every method call will be completed, even if interrupted by another
recursive call.
(C) Incomplete recursive calls are executed in a LIFO sequence.
(D) All of the above

94. Consider the count method below.

Public static void count(int a, int b)


{
if (a <= b)
{
System.out.print(a + " ");
Count(a+1, b);
}
}

What will be displayed by the method call count(10,20)?

(A) 10 11 12 13 14 15 16 17 18 19 20
(B) 11 12 13 14 15 16 17 18 19 20
(C) 20 19 18 17 16 15 14 13 12 11 10
(D) 20 19 18 17 16 15 14 13 12 11

95. Consider the count method below.

Public static void count(int a, int b)


{
if (a <= b)
{
count(a+1, b);
System.out.print(a + " ");
}
}

What will be displayed by the method call count(10,20)?

(A) 10 11 12 13 14 15 16 17 18 19 20
(B) 11 12 13 14 15 16 17 18 19 20
(C) 20 19 18 17 16 15 14 13 12 11 10
(D) 20 19 18 17 16 15 14 13 12 11

96. What value will be returned by the call m09(5) ?

public static int m09 (int n)


{
if (n == 1)
return 25;
else
return m09(n-1);
}

(A) 1
(B) 5
(C) 25
(D) 125

97. What value will be returned by the call m10(5) ?

public static int m10 (int n)


{
if (n == 1)
return 25;
else
return n + m10(n-1);
}

(A) 5
(B) 39
(C) 125
(D) 195

98. What value will be returned by the call m11(1) ?

public static int m11(int n)


{
if (n == 5)
return 0;
else
return n + m11(n+1);
}

(A) 1
(B) 10
(C) 50
(D) 75

99. What value will be returned by the call m13(5) ?

public static int m13 (int n)


{
if (n == 1)
return 1;
else
return n * m13(n-1);
}

(A) 5
(B) 20
(C) 24
(D) 120

100. What value will be returned by the call m14(5,6)?

public static int m14 (int a, int b)


{
if (a == 0)
return 0;
else
return b + m14(a-1,b);
}

(A) 1
(B) 11
(C) 30
(D) 120

You might also like