MCQ Java
MCQ Java
1. Given the following code how could you invoke the Base constructor that
will print out the string "base constructor";
public Sup(){
//Two
}
2. Which of the following method is not valid when inserted in place of the
comment //Method Here?
class Base{
void amethod(int i) { }
}
a) void amethod(int i) { }
b) protected void amethod(int i) { }
c) public void amethod(int i) { }
d) private void amethod(int i) { }
3. What is wrong with the following code?
class MyException extends Exception {}
public class Example{
public void doTask() {
try {
bar( );
} finally {
baz( );
} catch (MyException e) {}
}
public void bar( ) throws MyException {
throw new MyException();
}
public void baz( ) throws RuntimeException {
throw new RuntimeException();
}
}
a. Since the method foo( ) does not catch the exception generated by the
method baz( ), it must declare the RuntimeException in a throws clause.
b. A try block cannot be followed by both a catch and a finally block.
c. An empty catch block is not allowed.
d. A catch block cannot follow a finally block.
e. A finally block must always follow one or more catch blocks.
7) Given:
Set<String> set = new TreeSet<>();
set.add("E");
set.add("D");
set.add("E");
set.add("B");
set.add("A");
for(String s : set) {
System.out.print(s + " ");
}
What is the output of executing above code?
a) ABDE
b) EDEBA
c) EDBA
d) ABDEE
new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
}
a) (String o1, String o2) ->{ return o1.length() - o2.length();}
b) (o1, o2) ->{ return o1.length() - o2.length(); }
c) (o1, o2) -> o1.length() - o2.length();
d) ALL
9) You want to find out the value of the last element of an array.
You write the following code. What will happen when you compile and run it.?
public class Test{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
a) An error at compile time
b) An error at run time
c) The value 0 will be output
d) The string "null" will be output
10) What is the value of "total" variable after executing below code?
int total = Arrays.stream(new int[]{ 1, 2, 3 })
.filter(i -> i >= 2)
.map(i -> i * 3)
.sum();
a) 5
b) 18
c) 15
d) 9
11) Which among the listed methods does not belong to java.lang.Object class?
a) String toString()
b) boolean equals(Object o)
c) Class getClass()
d) int compareTo(Object o)
List<Integer> l = Arrays.asList(ints);
l.forEach(/*here*/);
Which of the following lines must be placed in place of /*here*/ for the number sequence to be
printed?
A. System::out::println
B. System::out.println
C. System.out.println
D. System.out::println
17) Which keyword is used to mark critical section so that at any point only one thread can access
critical section?
a) synchronized
b) transient
c) lock
d) static
a) executeUpdate( )
b) executeQuery( )
c) getResultSet( )
d) getData( )
b. False
20) Which of the following is used to determine at which point an annotation metadata should be
discarded?
a) @Target
b) @Retention
c) @Element