0% found this document useful (0 votes)
36 views8 pages

Java Exception Handling MCQs

The document contains multiple-choice questions (MCQs) related to Java programming concepts, including constructors, method overriding, exception handling, interfaces, collections, and threading. Each question presents a scenario or code snippet, followed by options for the correct answer. The questions cover fundamental Java principles and common programming practices.

Uploaded by

Suresh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

Java Exception Handling MCQs

The document contains multiple-choice questions (MCQs) related to Java programming concepts, including constructors, method overriding, exception handling, interfaces, collections, and threading. Each question presents a scenario or code snippet, followed by options for the correct answer. The questions cover fundamental Java principles and common programming practices.

Uploaded by

Suresh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MCQ Java

1. Given the following code how could you invoke the Base constructor that
will print out the string "base constructor";

public class Base {


public Base(int i){
[Link]("base constructor");
}
public Base(){
}
}

public class Sup extends Base{


public static void main(String argv[]){
Sup s= new Sup();
//One
}

public Sup(){
//Two
}

public void derived(){


//Three
}
}

a) On the line After //One put Base(10);


b) On the line After //One put super(10);
c) On the line After //Two put super(10);
d) On the line After //Three put super(10);

2. Which of the following method is not valid when inserted in place of the
comment //Method Here?

class Base{
void amethod(int i) { }
}

public class Derived extends Base{


//Method Here
}

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.

4. Given the following interface definition, which definition is valid?


interface I {
void setValue(int val);
int getValue();
}
a. class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}
b. abstract class C implements I {
int getValue() { return 0; }
abstract void increment( );
}
c. interface D implements I {
void increment();
}
d. class E implements I {
int value;
public void setValue(int val) {
value = val;
}
}
5. Given:
public class Person {
private int id;
private String name;
// appropriate constructors
// setters and getters
}
List<Person> persons = new ArrayList<Person>();
[Link](new Person(100,"Smith"));
[Link](new Person(120,"Allan"));
[Link](new Person(301,"Barry"));
[Link](new Person(101,"Kate"));
[Link](persons);
for(Person person: persons) {
[Link]([Link]());
}
What is the output of the above code?
a) Compilation Error
b) Prints: 100, 101, 120 and 301
c) Prints: 120,301,101 and 100
d) Runtime Exception
6) What is the result of compiling and executing the following program.
abstract class Account {
public String getAccountNo() {
return "SB100";
}
}
class SavingsAccount extends Account {
public double getBalance() {
return 0.0;
}
}

public class Test {


public static void main(String peace[]) {
Account account= new SavingsAccount();
[Link]([Link]());
}
}
a) Compilation error for line [Account account= new SavingsAccount(); ]
b) Compilation error for line [[Link]([Link]()); ]
c) Prints: 0.0
d) Exception during runtime

7) Given:
Set<String> set = new TreeSet<>();
[Link]("E");
[Link]("D");
[Link]("E");
[Link]("B");
[Link]("A");

for(String s : set) {
[Link](s + " ");
}
What is the output of executing above code?

a) ABDE
b) EDEBA
c) EDBA
d) ABDEE

8) Which of the following are equivalent to the below code?

new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return [Link]() - [Link]();
}
}
a) (String o1, String o2) ->{ return [Link]() - [Link]();}
b) (o1, o2) ->{ return [Link]() - [Link](); }
c) (o1, o2) -> [Link]() - [Link]();
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];
[Link](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 = [Link](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 [Link] class?
a) String toString()
b) boolean equals(Object o)
c) Class getClass()
d) int compareTo(Object o)

12) State True or False: static variables reside on Heap?


a) TRUE
b) FALSE
13) Which method is used by the caller thread to wait for other threads to finish
its execution?
a) sleep(long ms)
b) wait()
c) join()
d) suspend()

14) Which method of Object is used to release the lock/monitor/mutex?


a) suspend()
b) sleep(long ms)
c) wait()
d) release()
15) State True or False: Thread class implements Runnable?
a) TRUE
b) FALSE

16) Given the following code:


public static void main(String[] args) {

Integer ints[] = {1,2,3,4,5,6,7};

List<Integer> l = [Link](ints);

[Link](/*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::[Link]

C. [Link]

D. [Link]::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

18) Which method returns [Link]?

a) executeUpdate( )

b) executeQuery( )

c) getResultSet( )

d) getData( )

19) State True or False: ArithmeticException is a CheckedException


a. True

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

d) We can’t discard annotation metadata

Common questions

Powered by AI

The 'persons' list does not sort the objects according to 'id' because the 'Person' class does not implement the 'Comparable' interface and lacks a 'compareTo' method to determine the sorting order .

The problem with the code is that a catch block cannot follow a finally block. The correct order should be try-catch-finally .

The comparator can be refactored using the lambda expression '(o1, o2) -> o1.length() - o2.length();'. This is correctly simplified from the anonymous inner class syntax .

Running the code will result in an error at runtime. The array index tries to access the element at position 5, which is beyond its actual size leading to an ArrayIndexOutOfBoundsException .

A compilation error occurs because the 'Account' class does not have a method called 'getBalance()'. This method is specific to the 'SavingsAccount' subclass .

The '@Retention' annotation determines at what point annotation metadata should be discarded, with options like retention in source code, class files, or runtime .

You can invoke the 'Base' constructor that prints "base constructor" by inserting 'super(10);' on the line marked as //Two within the 'Sup' constructor .

'ArithmeticException' is an unchecked exception in Java, meaning it does not need to be declared in a method's 'throws' clause, contrary to checked exceptions .

The correct line to use in place of /*here*/ is 'System.out::println'. This uses a method reference to print each element of the sequence .

The output will be 'A B D E'. The TreeSet automatically sorts its elements in natural ascending order and does not allow duplicate elements, so the repeated 'E' is omitted .

You might also like