MCQs
Total Questions : 61
| Page 2 of 7 pages
Question 11. Given the following piece of code:
class SalaryCalculationException extends Exception{}
class Person{
public void calculateSalary() throws SalaryCalculationException{
//...
throw new SalaryCalculationException();
//...
}
}
class Company{
public void paySalaries(){
new Person().calculateSalary();
}
}
Which of the following statements is correct?
1. This code will compile without any problems.
2. This code will compile if in method paySalaries() we return a boolean in stead of void.
3. This code will compile if we add a try-catch block in paySalaries().
4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries().
class SalaryCalculationException extends Exception{}
class Person{
public void calculateSalary() throws SalaryCalculationException{
//...
throw new SalaryCalculationException();
//...
}
}
class Company{
public void paySalaries(){
new Person().calculateSalary();
}
}
Which of the following statements is correct?
1. This code will compile without any problems.
2. This code will compile if in method paySalaries() we return a boolean in stead of void.
3. This code will compile if we add a try-catch block in paySalaries().
4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries().
Answer: Option D. -> 3 and 4
Question 12. What will be the output of the following piece of code:
class Person{
public void talk() {}
}
public class Test{
public static void main(String args[]){
Person p = null;
try{
p.talk();
}
catch(NullPointerException e){
System.out.print("There is a NullPointerException. ");
}
catch(Exception e){
System.out.print("There is an Exception. ");
}
System.out.print("Everything went fine. ");
}
}
class Person{
public void talk() {}
}
public class Test{
public static void main(String args[]){
Person p = null;
try{
p.talk();
}
catch(NullPointerException e){
System.out.print("There is a NullPointerException. ");
}
catch(Exception e){
System.out.print("There is an Exception. ");
}
System.out.print("Everything went fine. ");
}
}
Answer: Option A. -> There is a NullPointerException. Everything went fine.
Answer: Option A. -> 3
Answer: Option B. -> NumberFormatException
Answer: Option C. -> One Two
Question 16. What will be the result after compiling this code?
class SuperClass{
public int doIt(String str, Integer... data)throws Exception{
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class Test extends SuperClass{
public int doIt(String str, Integer... data){
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " +signature);
return 0;
}
public static void main(String... args){
SuperClass sb = new Test();
sb.doIt("hello", 3);
}
}
class SuperClass{
public int doIt(String str, Integer... data)throws Exception{
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class Test extends SuperClass{
public int doIt(String str, Integer... data){
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " +signature);
return 0;
}
public static void main(String... args){
SuperClass sb = new Test();
sb.doIt("hello", 3);
}
}
Answer: Option C. -> Compilation fails
Answer: Option C. -> Compilation Error
Question 18. What is the output for the below code ?
import java.io.FileNotFoundException;
class A{
public void printName() throws FileNotFoundException{
System.out.println("Value-A");
}
}
class B extends A{
public void printName() throws NullPointerException{
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args) throws Exception{
A a = new B();
a.printName();
}
}
import java.io.FileNotFoundException;
class A{
public void printName() throws FileNotFoundException{
System.out.println("Value-A");
}
}
class B extends A{
public void printName() throws NullPointerException{
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args) throws Exception{
A a = new B();
a.printName();
}
}
Answer: Option C. -> Name-B
Question 19. What will be the result of executing the following code?
public class Test{
public void divide(int a, int b){
try{
int c = a / b;
}catch(Exception e){
System.out.print("Exception ");
}finally{
System.out.println("Finally");
}
public static void main(String args[]){
Test t = new Test();
t.divide(0,3);
}
}
public class Test{
public void divide(int a, int b){
try{
int c = a / b;
}catch(Exception e){
System.out.print("Exception ");
}finally{
System.out.println("Finally");
}
public static void main(String args[]){
Test t = new Test();
t.divide(0,3);
}
}
Answer: Option D. -> Prints out: Finally
Answer: Option B. -> A and B