This document contains 26 multiple choice questions related to Java programming concepts such as classes, interfaces, generics, recursion, data structures, and algorithms. Some key points covered include:
- Declaring a generic class and interface using angle brackets
- Creating an Action object to represent an event like opening a file
- Using recursion to print nested brackets for different input values
- Implementing stacks and queues using different data structures
- The base case and recursive calls in a recursive factorial function
- Checking for errors in a method to move a node in a doubly linked list
The questions test understanding of Java fundamentals like overloaded methods, exception handling, switch statements, and complexity analysis of recursive vs iterative algorithms. Correct answers
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
Java Class Questions Multiple Choice Quiz
1. Question 1 To declare a class named A with a generic type, use a. public class A { ... } b. public
class A { ... } c. public class A(E) { ... } d. public class A(E, F) { ... } Select one: a. b. c. d.
Question 2 Answer saved Marked out of 1.00 Flag question Question text What does the
following code do? Action openAction = new AbstractAction( "Open..." ) { public void
actionPerformed( ActionEvent e ) { doOpen(); } }; JButton openButton = new JButton(
openAction ); JMenuItem openCommand = new JMenuItem( openAction ); Select one or more:
a. This code creates an Action that represents the opening of a file in the doOpen() instance
method. b. This code creates a button from the Action. c. This code creates a menu item from the
Action. d. This code reads a text file.
Question 3 Answer saved Marked out of 1.00 Flag question Question text Given the following
code: static void showOutput(int mark) { if (mark == 0) { System.out.print("*"); } else {
System.out.print("["); showOutput(mark - 1); System.out.print(","); showOutput(mark - 1);
System.out.println("]"); } } Can you determine what is produced by the following subroutine
calls: showOutput(0), showOutput(1), showOutput(2), and showOutput(3)? a. showOutput(0)
outputs: * showOutput(1) outputs: [*,*] showOutput(2) outputs: [[*,*],[*,*]] showOutput(3)
outputs: [[[*,*],[*,*]],[[*,*],[*,*]]] b. showOutput(0) outputs: [ showOutput(1) outputs: *,*
showOutput(2) outputs: [[],[]] showOutput(3) outputs: [[[*,*],[*,*]],[[*,*],[*,*]]] Select one: a.
b.
Question 4 Answer saved Marked out of 1.00 Flag question Question text A switch statement,
most often has the form: switch (expression) { case constant-1: statements-1 break; … } The
value of the expression can be: i. int ii. short iii. byte iv. Primitive char v. Enum vi. String vii.
Real number Select one: a. iii , iv and v b. i , ii, iii and iv c. All, except vi and vii d. vi and vii e.
All of the types listed
Question 5 Answer saved Marked out of 1.00 Flag question Question text Which of the
following statements are true? Select one or more: a. Recursive methods usually takes more
memory space than non-recursive methods. b. A recursive method can always be replaced by a
non-recursive method. c. In some cases, however, using recursion enables you to give a natural,
straightforward, simple solution to a program that would otherwise be difficult to solve. d.
Recursive methods run faster than non-recursive methods.
Question 6 Answer saved Marked out of 1.00 Flag question Question text Which of the
following are true? Select one or more: a. A stack can be viewed as a special type of list, where
the elements are accessed, inserted, and deleted only from the end, called the top, of the stack. b.
A queue represents a waiting list. A queue can be viewed as a special type of list, where the
elements are inserted into the end (tail) of the queue, and are accessed and deleted from the
beginning (head) of the queue. c. Since the insertion and deletion operations on a stack are made
2. only at the end of the stack, using an array list to implement a stack is more efficient than a
linked list. d. Since deletions are made at the beginning of the list, it is more efficient to
implement a queue using a linked list than an array list.
Question 7 Answer saved Marked out of 1.00 Flag question Question text Suppose the rule at the
office is that the workers who arrive later will leave earlier. Which data structure is appropriate
to store the workers? Select one: a. Stack b. Queue c. Array List d. Linked List
Question 8 Answer saved Marked out of 1.00 Flag question Question text To declare an interface
named A with two generic types, use a. public interface A { ... } b. public interface A { ... } c.
public interface A(E) { ... } d. public interface A(E, F) { ... } Select one: a. b. c. d.
Question 9 Not yet answered Marked out of 1.00 Flag question Question text To create a list to
store integers, use a. ArrayListlist = new ArrayList(); b. ArrayList list = new ArrayList(); c.
ArrayList list = new ArrayList(); d. ArrayList list = new ArrayList(); Select one: a. b. c. d.
Question 10 Answer saved Marked out of 1.00 Flag question Question text What is the printout
of the following code? List list = new ArrayList(); list.add("A"); list.add("B"); list.add("C");
list.add("D"); for (int i = 0; i < list.size(); i++) System.out.print(list.remove(i)); Select one: a.
ABCD b. AB c. AC d. AD e. ABC True or False: If 'currentCell' is in the middle of the list this
moveRight() method will perform its duties correctly. Select one: True False
Question 11 Answer saved Marked out of 1.00 Flag question Question text The server listens for
a connection request from a client using the following statement: Select one: a. Socket s = new
Socket(ServerName, port); b. Socket s = serverSocket.getSocket() c. Socket s = new
Socket(ServerName); d. Socket s = serverSocket.accept()
Question 12 Answer saved Marked out of 1.00 Flag question Question text Which of the
following code is correct to obtain hour from a Calendar object cal? Select one: a.
cal.get(Calendar.HOUR); b. cal.getHour(); c. cal.hour(); d. cal.get(Hour); Information Flag
question Information text The next two questions refer to this method: public void
switchMethod(String input){ switch(input){ case "apple": System.out.println("sauce"); break;
case "orange": System.out.println("julius"); case "banana": System.out.println("split");
break; case "pear": System.out.println("juice"); default: System.out.println(input); }
Question 13 Answer saved Marked out of 1.00 Flag question Question text What is the output to
System out after the following method call: switchMethod("orange"); Select one: a. sauce b.
julius c. split d. juice e. None of the Above
Question 14 Answer saved Marked out of 1.00 Flag question Question text What is the output to
System out after the following method call: switchMethod("strawberry"); Select one: a. sauce
b. julius c. juice d. strawberry e. None of the Above Information Flag question Information text
The next two questions refer to the following: 1 public int factorial(int number){ 2
if(number==1) { 3 return 1; 4 } else { 5 return number*(factorial(number-1)); 6 } 7 }
3. Question 15 Answer saved Marked out of 1.00 Flag question Question text On which line is the
base case for this recursive function? Select one: a. 1 b. 2 c. 4 d. 5 e. None of the Above
Question 16 Answer saved Marked out of 1.00 Flag question
Question text On which line is the recursive call for this recursive function? Select one: a. 1 b. 2
c. 4 d. 5 e. None of the Above Information Flag question Information text The next two
questions refer to the following: Cell class reference: public class Cell { public Cell(){ } public
char content; // The character in this cell. public Cell next; // Pointer to the cell to the right of this
one. public Cell prev; // Pointer to the cell to the left of this one. } The following method is
supposed to move one node to the right in a doubly linked list of Cells but it contains a flaw. The
global variable 'currentCell' is a pointer to a Cell in the list. 1 public void moveRight() { 2 if
(currentCell.next == null) { 3 Cell newCell = new Cell(); 4 newCell.content = ' '; 5
newCell.next = null; 6 newCell.prev = currentCell; 7 currentCell.next = newCell; 8 currentCell =
currentCell.next; 9 } 10 }
Question 17 Answer saved Marked out of 1.00 Flag question Question text True or False: If
'currentCell' is at the head of the list this moveRight() method will perform its duties correctly.
Select one: True False
Question 18 Answer saved Marked out of 1.00 Flag question Question text True or False: If
'currentCell' is in the middle of the list this moveRight() method will perform its duties
correctly. Select one: True False
Question 19 Not yet answered Marked out of 1.00 Flag question Question text Sample class
reference: public class Sample { private int number; private String color; public Sample(int
number) { this.number = number; this.color = "blue"; } @Override public boolean
equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return
false; } final Sample other = (Sample) obj; if ((this.color == null) ? (other.color != null) :
!this.color.equals(other.color)) { return false; } return true; } } Given the above code for the
Sample class what is the output of the following program: public static void main(String args[]){
List list = new ArrayList(); Sample sample1 = new Sample(1); Sample sample2 = new
Sample(2); list.add(sample1); Boolean contains; contains = list.contains(sample2);
System.out.println("Contains: " + contains); } Select one: a. Contains: true b. Contains: false c.
Contains: null
Question 20 Answer saved Marked out of 1.00 Flag question Question text True or False: A Set
is used to store objects in a particular order. Select one: True False
Question 21 Answer saved Marked out of 1.00 Flag question Question text True or False: The
following two statements will always yield the same value when a and b are Strings: 1)
a.equals(b); 2) a==b; Select one: True False
Question 22 Answer saved Marked out of 1.00 Flag question Question text Overloaded methods
4. must be differentiated by: Select one: a. method name b. data type of arguments c. method
signature d. None of the above
Question 23 Answer saved Marked out of 1.00 Flag question Question text True or False: An
Interface contains method declarations as well as implementations. Select one: True False
Question 24 Answer saved Marked out of 1.00 Flag question Question text True or False: A
class that implements an interface may only implement a few of that interface's method
declarations. Select one: True False
Question 25 Answer saved Marked out of 1.00 Flag question Question text For the following list:
int[] list = { 4, 8, 10, 6, 2, 8, 5 }; What would the list look like after one pass of the outer loop of
the bubble sort algorithm? Select one: a. { 2, 4, 5, 6, 8, 8, 10 } b. { 4, 8, 8, 6, 2, 10, 5 } c. { 4, 8,
6, 2, 8, 5, 10 } d. None of the above Question
26 Answer saved Marked out of 1.00 Flag question Question text True or False: Every 'try'
block must end with a 'finally' block. Select one: True False Question
27 Answer saved Marked out of 1.00 Flag question Question text If A and B are logical
expressions: !(A && B) is equivalent to: Select one: a. (A || B) b. (!A || !B) c. (!A && !B) d.
None of the above
Solution
Please follow the data and description :
1)
A generic class is defined as, class name { /* ... */ }
The type parameter section, delimited by angle brackets (<>), follows the class name. It
specifies the type parameters (also called type variables) T1, T2, ..., and Tn.
So the answer is OPTION C ( public class A(E) { ... } ).
2)
The class that we are writing has an instance method doOpen() (with no parameters) that is
meant to be used to open a file selected by the user. This creates an Action that represents the
action of opening a file.
So the answer is OPTION C (This code creates a menu item from the Action. ).
3)
The output for the code given is
showOutput(0) outputs: *
showOutput(1) outputs: [*,*]
showOutput(2) outputs: [[*,*],[*,*]]
showOutput(3) outputs: [[[*,*],[*,*]],[[*,*],[*,*]]]
5. So the answer is OPTION A (showOutput(0) outputs: * showOutput(1) outputs: [*,*]
showOutput(2) outputs: [[*,*],[*,*]] showOutput(3) outputs: [[[*,*],[*,*]],[[*,*],[*,*]]] ).
4)
The expression used in a switch statement must have an integral or enumerated type, or be of a
class type in which the class has a single conversion function to an integral or enumerated type.
So the answer is OPTION E (All of the types listed).
5)
a. Recursive methods usually takes more memory space than non-recursive methods.
b. A recursive method can always be replaced by a non-recursive method.
c. In some cases, however, using recursion enables you to give a natural, straightforward, simple
solution to a program that would otherwise be difficult to solve.
d. Recursive methods run faster than non-recursive methods.
All the answers are true in case of a recursion.
6)
a. A stack can be viewed as a special type of list, where the elements are accessed, inserted, and
deleted only from the end, called the top, of the stack.
b. A queue represents a waiting list. A queue can be viewed as a special type of list, where the
elements are inserted into the end (tail) of the queue, and are accessed and deleted from the
beginning (head) of the queue.
c. Since the insertion and deletion operations on a stack are made only at the end of the stack,
using an array list to implement a stack is more efficient than a linked list.
d. Since deletions are made at the beginning of the list, it is more efficient to implement a queue
using a linked list than an array list.
All the answers are true in case of a queue and a stack.
7)
Stack : a stack is a collection of objects in which objects are accessed in LIFO (Last In First Out)
fashion.
So the answer is OPTION A (Stack).
8)
public interface A(E, F) { ... } is an interface with a two generic types.
So the answer is OPTION D (public interface A(E, F) { ... } ).
9)
To declare a list of integer type we can write the line as ArrayList list = new ArrayList();
So the answer is OPTION C (ArrayList list = new ArrayList();).
6. 10)
CODE :
public static void main(String[] args) {
List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.remove(i));
}
}
OUTPUT :
AC
So the answer is OPTION C (AC).
If 'currentCell' is in the middle of the list this moveRight() method will perform its duties
correctly it is true.
11)
The server listens for a connection request from a client using the following statement
Socket s = serverSocket.accept();
So the answer is OPTION D (Socket s = serverSocket.accept());
12)
The following code is correct to obtain hour from a Calendar object cal :
cal.get(Calendar.HOUR);
So the answer is OPTION A (cal.get(Calendar.HOUR););
13)
For the given the code the output for the call switchMethod("orange"); is given by julius.
So the answer is OPTION B (julius).
14)
For the given the code the output for the call switchMethod("strawberry"); is given by
strawberry.
So the answer is OPTION D (strawberry).
7. 15)
1 public int factorial(int number){
2 if(number==1) {
3 return 1;
4 } else {
5 return number*(factorial(number-1));
6 }
7 }
The base case for the recursive function for the given code is in the line 5. So the answer is
OPTION D (5);
16)
The call for the recursive function for the given code is in the line 5. So the answer is OPTION D
(5);
17)
If 'currentCell' is at the head of the list this moveRight() method will perform its duties
correctly. It is True. So the answer is OPTION A (True).
18)
If 'currentCell' is in the middle of the list this moveRight() method will perform its duties
correctly. It is True. So the answer is OPTION A (True).
19)
CODE :
public class Sample {
private int number;
private String color;
public Sample(int number) {
this.number = number;
this.color = "blue";
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
8. if (getClass() != obj.getClass()) {
return false;
}
final Sample other = (Sample) obj;
if ((this.color == null) ? (other.color != null) : !this.color.equals(other.color)) {
return false;
}
return true;
}
public static void main(String args[]) {
List list = new ArrayList();
Sample sample1 = new Sample(1);
Sample sample2 = new Sample(2);
list.add(sample1);
Boolean contains;
contains = list.contains(sample2);
System.out.println("Contains: " + contains);
}
}
OUTPUT :
Contains: true
So the answer is OPTION A (Contains: true).
20)
A Set is used to store objects in a particular order.
It is True.
So the answer is OPTION A (True).
21)
The two statements will always yield the same value when a and b are Strings: 1) a.equals(b); 2)
a==b; It is True.
So the answer is OPTION A (True).
22)
If a class have multiple methods by same name but different parameters, it is known as Method
Overloading.
9. There are two ways to overload the method in java
By changing number of arguments
By changing the data type
So the answer is OPTION B (data type of arguments).
23)
Interfaces are declared using the interface keyword, and may only contain method signature and
constant declarations (variable declarations that are declared to be both static and final ). All
methods of an Interface do not contain implementation (method bodies).
So the answer is OPTION B (False).
24)
A class that implements an interface may only implement a few of that interface's method
declarations. No this is False.
So the answer is OPTION B (False).
25)
The output for the list = { 4, 8, 10, 6, 2, 8, 5 }; after the outer loop is {4, 8, 6, 10, 2, 5, 8}
So the answer is OPTION D (None of the Above).
26)
Every 'try' block must end with a 'finally' block. Not necessarily in evry case but could be also
a catch block of statement. So the answer is OPTION B (False).
27)
!(A && B) is equivalent to (!A || !B)
So the answer is OPTION B ((!A || !B) ).
Hope this is helpful.