Ticker

6/recent/ticker-posts

Java Programming Practice MCQs Codetantra. CBT-5

 Java Programming Practice MCQs Codetantra. CBT-5

  • Lambda Expressions
  • Date Time (Utility Classes)
  • Exception Handling

01. Choose the correct option for the code given below:

class Main {
    String dummy = "5";
    void property1() {
        try {
            dummy = dummy + "11";
            property2();
        }
        catch (Exception exc) {
            dummy = dummy + "29";
        }
    }
 
    void property2() throws Exception {
        try {
            dummy = dummy + "39";
            property3();
        }
        catch(Exception e) {
            throw new Exception();
        }
        finally {
            dummy = dummy + "55";
        }
 
        dummy = dummy + "173";
 
    }
     
    void property3() throws Exception {
        throw new Exception();
    }
 
    void showTime() {
        System.out.println(dummy);
    }
 
    public static void main(String[] args) {
        Main mainObject = new Main();
        mainObject.property1();
        mainObject.showTime();
    }
 
}
 
  
  •  

    Code prints:

    511395529

  •  

    Code prints:

    5113955173

  •  

    Code prints:

    5113929

  •  

    Code prints:

    511395517329


02. Choose the correct option for the code given below:

class Main {   
    int dummy = 65;
    void compute() throws Exception {
        try {
            dummy += 3;
            try {
                dummy += 3;
                try {
                    dummy += 3;
                    throw new Exception();
                    
                }
                catch(Exception exc) {
                    dummy += 3;
                    throw new Exception();
                }
            }
            catch(Exception ex) {
                dummy += 3;
            }
        }
        catch(Exception ex) {
            dummy += 3;
        }
 
    }
 
    void showTime() {
        System.out.println(dummy);
    }
 
    public static void main(String[] args) throws Exception {
       Main object = new Main();
        object.compute();
        object.showTime();
    }
}
  •  

    80

  •  

    83

  •  

    Error-> 'try' without 'catch', 'finally' or resource declarations


  •  

    77


03. Find out the correct results of given code snippet:

import java.util.*;
class Main {
public static void main(String args[]) {
		int array1[] = { 3, 52, 10, 25, 42, 18, 35 };
		int array2[] = Arrays.copyOfRange(array1, 2, 6);
		for (int i : array2)
			System.out.print(i + " ");
    	System.out.println();
    	int[] array3 = Arrays.copyOfRange(array1, 8, array1.length + 3);
		for (int i : array3)
			System.out.print(i + " ");
	}
}

  • 10 25 42 18 

    0 0 0

  •  

    10 25 42 18 35

    35 0 0

  •  

    Code throws  IIlegalArgumentException

  •  

    Code throws ArrayIndexOutOfBoundsException


04. Predict the right option for the below given code snippet.

public class Main {
     static int x, y;
     public static void main(String args[]) {
         try{
             A();
             x = 10; y = 3;
             
         }
         catch(RuntimeException ex) {
             System.out.print("81"); 
             
         }
         catch(Exception ex) { 
             System.out.print("67"); 
             
         }
         System.out.print("54");
         
     }
     private static void A() {
         try {       
             B();    
             
         }
         finally { 
             System.out.print("8");  
         }
     }
        
      private static void B() {
          try { 
              int res = x/y;
              System.out.print(res); 
	     }
	     catch(NumberFormatException | NullPointerException ex) {
	         System.out.print("39");
	           
	     }
            
      }
    
}


  •  

    88154

  •  

    86754

  •  

    39854

  •  

    Compilation Error


05. Predict the right option for the below given code snippet.

public class Main {
     static int x, y;
     public static void main(String args[]) {
         try{
             A();
             x = 10; y = 3;
             
         }
         catch(RuntimeException ex) {
             System.out.print("92"); 
             
         }
         catch(Exception ex) { 
             System.out.print("78"); 
             
         }
         System.out.print("45");
         
     }
     private static void A() {
         try {       
             B();    
             
         }
         finally { 
             System.out.print("20");  
         }
     }
        
      private static void B() {
          try { 
              int res = x/y;
              System.out.print(res); 
	     }
	     catch(NumberFormatException | ArithmeticException ex) {
	         System.out.print("31");
	           
	     }
            
      }
    
}


  •  

    209245

  •  

    207845

  •  

    312045

  •  

    Compilation Error


06. Choose the correct option for the code given below:

class Main {
   public static void main(String args[]) {
       try {
           int a = 82;
           throw ++a;
           
       }
       catch(int status) {
           System.out.println(status);
           
       }
       
   }
}


  •  

    82

  •  

    83

  •  

    No errors but nothing is going to be printed because catch will be bypassed for integer types

  •  

    Errors: Incompatible and unexpected types


07. Choose the correct option for the code given below:

class Main {
   public static void main(String args[]) { 
      try {
         throw new Exceptions();
      }
      catch(Exceptions t) {
         System.out.println("34");
      }
      finally {
         System.out.println("199");
      }
  }
}
class Exceptions extends Exception { 
    
}
  
  •  

    34

    199

  •  

    199

  •  

    34

  •  

    Error: Exceptions class is not found


08. Choose the correct option for the code given below:

class Main {
   public static void main(String args[]) { 
      try {
         throw new Exceptions();
      }
      catch(Exceptions t) {
         System.out.println("76");
      }
      finally {
         System.out.println("140");
      }
  }
}
class Exception extends Exceptions { 
    
}
  
  •  

    76

    140

  •  

    140

  •  

    76

  •  

    Error: Exceptions class is not found


09. Choose the correct option for the code given below:

public class Main {
  public static void main(String args[]) {
  
   try {
        int a = 34;
        int b = a * --a;
        if(b > (a * a))
            throw new B();
    }
    catch(A object1)     { 
       System.out.println("37"); 
    }
    catch(B object2)  { 
       System.out.println("70"); 
    }
    finally{
        System.out.println("102");
        
    }
  }
} 
class A extends Exception {
    
}
class B extends A  {
    
}
 
  
  •  

    Error that exception B has already been caught


  •  

    Clean code without errors and warnings print

    37

    102

  •  

    With one warning, Unreachable catch clause code prints

    37

    102

  •  

    With one warning, Unreachable catch clause code prints

    102

  •  

    Clean code without errors and warnings print

    102


10. Choose the correct option for the code given below:

class Main {
    public static void main(String[] args) {
        try {
            int array[]= {1, 18, 25, 32, 87};
            for (int i = 0; i <= 5; i++) {
                System.out.print(array[i] + " ");
            }
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("37");
        }
         
        catch (Exception e) {
            System.out.println (e);
        }
         
        
    }
}
 
  
  •  

    Error that exception B has already been caught

  •  

    Code prints:

    1 18 25 32 87 37

  •  

    With Out of bound exception code only prints

    37

  •  

    Code prints:

    1 18 25 32 87

  •  

    Error: exception ArrayIndexOutOfBoundsException has already been caught


11. Choose the correct option for the code given below:

class Main {
    public static void main(String[] args) {
        try {
            int array[]= {9, 16, 23, 49, 78};
            for (int i = 0; i <= 5; i++) {
                System.out.print(array[i] + " ");
            }
        }
        catch (Exception e) {
                  System.out.println (e);
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("21");
        }     
        
    }
}
 
  
  •  

    Error that exception B has already been caught

  •  

    Code prints:

    9 16 23 49 78 21

  •  

    With Out of bound exception code only prints

    21

  •  

    Code prints:

    9 16 23 49 78

  •  

    Error: exception ArrayIndexOutOfBoundsException has already been caught


12. Predict the correct output of below given code:

interface A {
	void function1(int x, int y);
	default void function2(int x, int y) {
	System.out.println(x + y);
	}
}
class Main {
	public static void main(String args[]) {
		A a = (int x, int y)->System.out.println(2*x + 2*y);
		a.function1(27,59);
		a.function2(41,74);
	}
}
  •  

    172

    115

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    172

  •  

    Error: Method function1(int,int) is not defined in interface A

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    115


13. Predict the correct output of below given code:

interface A {
	void function1(int x, int y);
	default void function2(int x, int y) {
	System.out.println(x + y);
	}
}
class Main {
	public static void main(String args[]) {
		A a = ()->System.out.println("8");
		a.function1();
		a.function2(22,54);
	}
}
  •  

    Error: incompatible types: incompatible parameter types in lambda expression


  •  

    8

    76

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    8

  •  

    Error: Method function1(int,int) is not defined in interface A

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    76


14. Predict the correct output of below given code:

interface A {
	void function1();
	default void function2(int x, int y) {
	System.out.println(x + y);
	}
}
class Main {
	public static void main(String args[]) {
		A a = ()->System.out.println("11");
		a.function1();
		a.function2(26,58);
	}
}
  •  

    Error: incompatible types: incompatible parameter types in lambda expression

  •  

    11

    84

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    11

  •  

    Error: Method function1(int,int) is not defined in interface A

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    84


15. Predict the correct output of below given code:

interface Interface {
	void property1(int x);
	default void property2(int x, int y ) {
	System.out.println(++x + y++);
	}


}
class Main {
	public static void main(String args[]) {
		Interface a = (int p)->System.out.println(p++);
		a.property1(11);
		a.property2(93,125);
	}
}
  •  

    Error: incompatible types: incompatible parameter types in lambda expression

  •  

    12

    219

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    11

  •  

    11

    219

  •  

    11

    218

  •  

    code throws a warning that method function1(int,int) is not defined in interface A, and print:

    12


16. Predict the right option for the given code snippet:


interface B {
     Number m(int x) {
         return x;
     }
}
public class Main {
    public static void main(String args[]) {
        B b = (int x) -> (9 * 2); 
        System.out.print(b.m());
    }
}
  • 2

  •  

    18

  •  

    Error: method m in interface B can be applied to given types

  •  

    9

  •  

    Error: interface B method m() is abstract and cannot have body


17. Predict the right option for the given code snippet:


interface Fruit {
     Number apple(int x);
}
public class Main {
    public static void main(String args[]) {
        Fruit b = (int x) -> (13 * 5); 
        System.out.print(b.apple(18));
    }
}
  •  

    18

  •  

    65

  •  

    Error: method apple in interface Fruit cannot be applied to given types

  •  

    324

  •  

    Error: interface Fruit method apple() body is not defined.


18. Predict the correct output of below given code snippet:

import java.util.*;
public class Main {
	public static void main(String[] args) {
		int array[] = {9, 15, 22, 13};
		int keyElement = 24;
		Arrays.fill(array, ++keyElement);
		System.out.println(Arrays.toString(array));
	}
}
  •  

    [24, 24, 24, 24]

  •  

    [25, 25, 25, 25]

  •  

    Error: cannot find symbol Arrays.fill(array, ++keyElement);

  •  

    [9, 15, 22, 13]


19. Predict the correct output of below given code snippet:

import java.util.*;
public class Main {
	public static void main(String[] args) {
		int array[] = { 14, 78, 42, 7, 71 };
		System.out.println(Arrays.toString(array));
		System.out.println(Arrays.toString(Arrays.copyOf(array, 8)));
	}
}
  •  

    [14, 78, 42, 7, 71]

    [14, 78, 42, 7, 71, 0, 0, 0]

  •  

    [14, 78, 42, 7, 71]

    [0, 0, 0, 0, 0, 0, 0, 0]

  •  

    [14, 78, 42, 7, 71]

    [0, 0, 0, 71, 7, 42, 78, 14]

  •  

    [14, 78, 42, 7, 71]

    [0, 0, 0, 14, 78, 42, 7, 71]


20. Predict the correct output of below given code snippet:

import java.util.*;
public class Main {
	public static void main(String[] args) {
		int array[] = { 264, 241, 128, 60, 42 };
		Arrays.sort(array, 1, 2);
		System.out.println(Arrays.toString(array));
	}
}

  •  

    [264, 241, 128, 60, 42]

  •  

    [42, 60, 128, 241, 264]

  •  

    error: cannot find symbol Arrays.sort(array);

  •  

    [241, 264, 128, 60, 42]


21. Predict the correct output of below given code snippet:

Assume current date: 1984-March-28

current time: 7:16:53 AM (HH:MM:SS)

import java.text.*;
import java.util.*;
class Main {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(DateFormat.getDateInstance().format(date));
		System.out.println(DateFormat.getTimeInstance().format(date));
		System.out.println(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date));
		System.out.println(DateFormat.getTimeInstance(DateFormat.SHORT).format(date));
	}
}

  •  

    Mar 28, 1984

    7:16:53 AM

    7:16:53 AM

    7:16 AM

  •  

    Mar 28, 1984

    7:16:53 AM

    7:16:53 AM

    7:16:53 AM

  •  

    1984/March/28

    7:16:53 AM

    7:16:53 AM

    7:16:53 AM

  •  

    1984-March-28

    7:16:53 AM

    7:16:53 AM

    7:16 AM


22. Predict the correct output of below given code snippet:

Assume current date: 2019-March-8

current time: 10:32:11 AM (HH:MM:SS)

import java.text.*;
import java.util.*;
class Main {
	public static void main(String[] args) {
		Date date = new Date();
        System.out.println(DateFormat.getDateTimeInstance().format(date));
		System.out.println(DateFormat.getTimeInstance(DateFormat.LONG).format(date));
	}
}

  •  

    Mar 8, 2019, 10:32:11 AM

    10:32:11 AM GMT

  •  

    Error: cannot find symbol System.out.println(DateFormat.getDateTimeInstance().format(date));


  •  

    Mar 8, 2019, 10:32:11 AM

    10:32:11

  •  

    Mar 8, 2019

    10:32:11 AM GMT


23. Analyse the code and select the correct option:

import java.util.Arrays;
public class Main {
	public static void main(String[] args) {
		int[] array1 = new int [] {1, 2, 1};
		int[] array2 = new int [] {1, 2, 1};
		Boolean a = Arrays.equals(array1, array2);
		if(a) {
		    System.out.print("102");
		}
		else {
		    System.out.print("184");
		}
	}
}


  •  

    102

  •  

    184

  •  

    incompatible types: boolean cannot be converted to int

  •  

    Error: cannot find symbol Boolean a = Arrays.equals(array1, array2);


24. Predict the right option for the below given code snippet.

class X extends Exception{ }
class Y extends X { }
public class Main {
     public static void main(String [] args) {
         try {    
             demo();     
             
         }
         catch (X obj) { 
             System.out.println (obj); 
             
         }
         
     } 
    public static void demo() throws Y {
        throw new Y();
        
    }
}

  •  

    X

  •  

    Y

  •  

    Compilation Error: Unreported Exception X

  •  

    Compilation Error: Unreported Exception Y



25. Predict the right option for the below given code snippet.

class X extends Exception{ }
class Y extends X { }
public class Main {
     public static void main(String [] args) {
         try {    
             demo();     
             
         }
         catch (X obj) { 
             System.out.println (obj); 
             
         }
         
     } 
    public static void demo() throws Y {
        throw new X();
        
    }
}

  •  

    X

  •  

    Y

  •  

    Compilation Error: Unreported Exception X

  •  

    Compilation Error: Unreported Exception Y




Post a Comment

0 Comments