Java Midterm Exam Questions & Answers

0% found this document useful (0 votes)
1K views5 pages
The document contains questions about Java programming concepts and code snippets with multiple choice answers. It covers topics like data types, operators, control flow, OOP concepts, excep…

Uploaded by

keeyeuu Xn vsh
  • Java Programming Quiz Part 1
  • Java Programming Quiz Part 2
  • Java Programming Quiz Part 3
  • Java Programming Quiz Part 5
  • Java Programming Quiz Part 4

1. What is the output from the following code snippet?

public class Test {


public static void main(String[] args) {
[Link](1 + 2 + "java" + 3);
}
}
__________________
The code will compile and print "3java3"

2. What is the output from the following code?

int x=5;
if (x>6)
System. out. println("x>l");
else if (x>=5)
System. out .println("x>=5");
else if (x<10)
System. out. println("x<10");
else
[Link]("none of the above");
____________________
x>=5

4. What is the output from the following code snippet?


String str1= "java";
String str2=new String("java");
[Link]( str1==str2 );
[Link]( str1==[Link]() );
______________________
The code will compile and print "false true"

5. The following code can be compiled, True/False?


byte b = 1;
b = b + 1;
___________________
false

6. What is the output from the following code?


int x=0;
int y=5;
do{
++x;
y--;
}while(x<3);
[Link](x + " " + y);
_______________
3 2

8. Interfaces define what?


______________________
All method definitions without any implementations

9. Modeling classes for a business problem requires understanding of the business


not Java. True or false?
______________________
false
10. The state of an object differentiates it from other objects of the same class.
True or False?
__________________________
true

11. In what order do multiple catch statements execute?


____________________________
The order they are declared in ( most specific first).

12. What symbol(s) is used to separate multiple exceptions in one catch statement?
_____________________________
A single vertical bar |

13. What is special about including a resource in a try statement?(Choose Two)


_____________________________
An error will be thrown if the resources does not open.

14. Which of the following statements is false?


____________________________
An ArrayList has a fixed length.

15. The main purpose of unit testing is to verify that an individual unit (a class,
in Java) is working correctly before it is combined with other components in the
system. True or false?
_______________________________
true

16. Unit testing is the phase in software testing in which individual software
modules are combined and tested as a whole. True or false?
_______________________________
false

17. Virtual method invocation must be defined with the instanceof operator.
True or false?
______________________________
false

18. You can always upcast a subclass to an interface provided you don't need to
access any members of the concrete class.
True or false?
___________________________
true

20. You can't downcast an object explicitly because you must use virtual method
invocation.
True or false?
________________________
false

22. The following code will compile.


class Node<T> implements Comparable<T>{
public int compareTo(T obj){return 1;}
}

class Test{
public static void main(String[] args){
Node<String> nc=new Node<>();
Comparable<String> com=nc;
}
________________________
true

23. Generic methods are required to be declared as static.


True or False?
______________________________
false

24. What is the result from the following code snippet?

public static void main(String[] args) {

List <Gum> list1 = new ArrayList<Gum>();

[Link](new Gum());

List list2 = list1;

[Link](new Integer(9));

[Link]([Link]());
}
____________________
2

25. < ? extends Animal > is an example of a bounded generic wildcard.


True or False?
__________________________
true

26. Stacks are identical to Queues.


True or false?
________________
false

27. A LinkedList is a list of elements that is dynamically stored.


True or false?
_______________
true

28. A LinkedList is a type of Stack.


True or False?
_______________________
true

29. What are maps that link a Key to a Value?


__________________
HashMaps

30. Which of the following describes a deque.


___________________
All of the above

31. Why might a sequential search be inefficient?


__________________________
It requires incrementing through the entire array in the worst case, which is
inefficient on large data sets.
32. Which of the following best describes lexicographical order?
____________________________
An order based on the ASCII value of characters.

33. Which of the following best describes lexicographical order?


________________________
An order based on the ASCII value of characters.

34. Which of the following is the correct lexicographical order for the conents of
the following int array?
____________________________
{1, 1, 17, 22, 28, 29, 3, 50, 71, 83}

35. A sequential search is an iteration through the array that stops at the index
where the desired element is found. True or false?
_________________________
true

36. Which is the correct way to initialize a HashSet?


_______________________
HashSet<String> classMates =
new HashSet<String>>();

37. What is the output from the following code snippet?


TreeSet <String> t = new TreeSet<String>();
if ([Link]("one"))
if ([Link]("two"))
if ([Link] ("three"))
[Link]("four");
for (String s : t)
[Link] (s);
__________________________
fouronethreetwo

38. Which of these could be a set? Why?


___________________________
{1, 2, 5, 178, 259} because it contains no duplicates and all its elements are of
the same type.

40. In a regular expression, {x} and {x,} represent the same thing, that the
preceding character may occur x or more times to create a match.
True or false?
__________________________
false

41. Consider that you are making a calendar and decide to write a segment of code
that returns true if the string month is April, May, June, or July. Which code
segment correctly implements use of regular expressions to complete this task?
__________________________
return [Link]("April|May|June|July");

42. A regular expression is a character or a sequence of characters that represent


a string or multiple strings.
True or false?
__________________________
true

43. Your teacher asks you to write a segment of code that returns true if String
str contains zero or one character(s) and false otherwise. Which of the following
code segments completes this task?(Choose Two)
____________________________
if( [Link]() == 0 || [Link]() == 1)
{ return true;}
return false; (*)

44. A non-linear recursive method can call how many copies of itself?
_______________________________
2 or more

45. A non-linear recursive method is less expensive than a linear recursive method.
True or false?
__________________
false

50. Identify the method, of those listed below, that is not available to both
StringBuilders and Strings?
_________________
delete(int start, int end)

Common questions

Powered by AI

The use of '? extends Animal' in Java generics signifies a bounded wildcard, allowing a collection that can hold instances of Animal or its subclasses. This allows flexibility in dealing with methods that operate on class hierarchies, ensuring type safety while providing abstraction over permissible objects .

Java's try-catch block prioritizes execution by matching exceptions with catch clauses from the most specific to the most general. This hierarchy ensures that the most relevant exception handler is executed first, avoiding bypassing more specific handlers, which prevents masking detailed exception information .

Non-linear recursive methods are expensive due to the number of recursive calls they make. Each state duplicates the call stack multiple times, often leading to exponential growth in time and space complexity compared to linear recursion, which maintains a singular, direct recursion path .

A sequential search is inefficient comparative to algorithms like binary search because it checks each element sequentially. In the worst case, it examines every element once, requiring O(n) time complexity which is suboptimal for large data sets .

In Java, a LinkedList is a list of elements stored dynamically, allowing efficient insertion and removal operations. It is not inherently a Stack, which follows LIFO (Last-In, First-Out) structure, although a LinkedList can be used to implement stack functionalities by managing end insertions and deletions manually .

Using an unbounded wildcard, such as '?', looses concrete type information, making it difficult to enforce specific type operations. While it provides flexibility, accessing and manipulating elements becomes error-prone due to loss of compile-time checks, diminishing effective type safety in operations like adding elements .

In Java, string concatenation involving integers follows the left-to-right evaluation order. For 'System.out.println(1 + 2 + "java" + 3);', the expression '1 + 2' is evaluated first as an integer operation resulting in '3'. Then, the result is concatenated with "java", forming "3java", and finally '3' is appended, yielding '3java3' .

In object-oriented programming, object equivalence is determined by overriding the 'equals()' method rather than using '==', which tests for reference equality. Proper implementation of 'equals()' ensures logical equality by comparing object states .

The intern() method is significant because it provides a canonical representation of the string. It checks if the string is already in the pool and, if not, adds it. This makes reference comparisons possible with '==', as interned strings can be checked directly against each other, even if they were initially created as separate objects .

The output 'false true' demonstrates the difference between reference equality and content equality in Java. When 'String str1 = "java";' and 'String str2 = new String("java");' are compared using '==', it checks for reference equality, resulting in 'false' because they are different objects. However, 'str1' and 'str2.intern()' are content-equal because 'intern()' returns a canonical representation for string content, hence the output 'true' .

1. What is the output from the following code snippet?
public class Test {
 public static void main(String[] args) {
  System
10. The state of an object differentiates it from other objects of the same class.
True or False?
__________________________
}
________________________
true
23. Generic methods are required to be declared as static.
True or False?
___________________
32. Which of the following best describes lexicographical order?
____________________________
An order based on the ASCII val
str contains zero or one character(s) and false otherwise. Which of the following 
code segments completes this task?(Choose

You might also like