Ticker

6/recent/ticker-posts

Java Programming Practice MCQs Codetantra. CBT-4

 Java Programming Practice MCQs Codetantra. CBT-4

  • Enumeration
  • Nested classes
  • final
  • StringBuilder class



01. Predict the results given by below given code snippet.

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class Main {
	public static void main(String[] args) {
		StringBuilder str1 = new StringBuilder("2379");
		System.out.println(str1.toStrings());
		StringBuilder str2 = str1.reverse();
		System.out.println(str2.toStrings());
		System.out.println(str1.toStrings());
	}
}



  •  

    2379

    9732

    2379

  • 2379

    2379

    2379 

  • 2379

    9732

    9732

  • Error: cannot find symbol System.out.println(str2.toStrings())


02. Analyze the below given code and choose the correct option.

enum Numbers {
	One,
	Two,
	Three;
	private Numbers() {
		System.out.println("Constructor id " + "257" + " is: "
						+ this.toString());
	}


	public void numberInfo() {
		System.out.println("578");
	}
}


public class Main {
	public static void main(String[] args)
	{
		Numbers n = Numbers.Two;
		System.out.println(n);
		n.numberInfo();
	}
}

 
  •  

    Constructor id 257 is: Two

    Constructor id 257 is: Three

    Two

    578

  •  

    Constructor id 257 is: Two

    Two

    578

  •  

    Constructor id 257 is: One

    Constructor id 257 is: Two

    Constructor id 257 is: Three

    Two

    578

  •  

    Error expected Two,

    Error expected Three,


03. Predict the results given by below given code snippet.

enum Laptops {
   MAC(2), LENOVO(4), DELL(6), ACER(9);
   private int pricing;
   Laptops(int pricing) {
      this.pricing = pricing;
   }
   public int fetchPrice() {
      return this.pricing;
   }
}
public class Main {
   public static void main(String args[]) {
      Laptops lappy[] = Laptops.values();
      for(Laptops lp: lappy) {
         System.out.println(lp.fetchPrice());
      }
   }
}

  • 9

    6

    4

    2

  • Error : illegal start of expression


  •  

    2

    4

    6

    9

  • error: cannot find symbol Laptops lappy[] = Laptops.values();

04. Analyze the below given code and choose the correct option.

public class Main {
	private int info = 398;
	
	class SubMain {
		private int info = 720;
		private int method() {
			return info;
		}
		public void main(String[] args) {
			SubMain sm = new SubMain();
			System.out.println(sm.method());
			
		}
	}
	private int method() {
		return info;
	}
	public static void main(String[] args) {
		Main m = new Main();
		Main.SubMain sm = m.new SubMain();
		System.out.printf("%d", m.method());
		sm.main(args);
	}
}
  •  

    398

  •  

    720

  • error: Illegal static declaration in inner class Main.SubMain

  • 398720

  •  

    398720398


05. Analyze the below given code and choose the correct option.

public class Main {
	private int methodA(int element) {
		class SubMain {
			private int methodB() {
				System.out.println("37");
				if(element < 69) {
					return 101;
				}
				else {
					return 183;
				}
			}
		}
		
		SubMain sm = new SubMain();
		return sm.methodB();
	}
	
	public static void main(String[] args) {
		Main m = new Main();
		System.out.println(m.methodA(96));
	}
}
  •  

    error: illegal start of expression class SubMain

  •  

    37

    183

  • 37

    101

  • error: cannot find symbol System.out.println(m.methodA(96));

06. Decode the code and choose the correct results:

abstract class A {
	final void showTime() {
		System.out.println("3572");
	}
}
class B extends A {
    static void funTime() {
        System.out.println("6144");
    }
}
public class Main {
	public static void main(String args[]) {
		A object = new B();
		object.showTime();
		object.funTime();
	}
}
  • 6144

    3572

  • 6144

    6144

  •  

    error: cannot find symbol object.funTime()

  •  

    3572

    6144

  • 3572

    3572


07. Predict the correct results of below given code snippet:

abstract class CT {
    public int x;
    CT() {
        x = 6;
    }
    abstract public void showTime();   
    abstract final public void funTime(); 
}
 
class Main extends CT {
    public void showTime(int a) {
        this.x = a;
    }
    final public void funTime() {
        System.out.println(x);
    }
    public static void main(String[] args) {
        Main object = new Main();
        object.showTime(169);
        object.funTime();
    }
}
  •  

    169

  • 6

    169

  •  

    6

  • Error

08. Predict the correct results of below given code snippet:

abstract class B {
	abstract void funTime();
}

class D extends B {
	final void funTime() {
		System.out.println("31");
	}
}

class Main {
	public static void main(String args[]) {
		B object = new D();
		object.funTime();
	}
}


  •  

    Error : overriding method is final

  •  

    Code executes without error without any output

  •  

    Clean code with 31 printed as output.

  •  

    Code gives warning due to use of final method , but also prints 31

09. Predict the correct results of below given code snippet:

import java.io.*;
interface a {
	final int x = 32;
	default void showTime2() {
	System.out.println("65");
	}
}
class Main implements a {
	public void showTime1() {
	System.out.println("15");
	}
	public static void main(String[] args) {
		Main test = new Main();
		test.showTime1();
		System.out.println(x);
		test.showTime2();
		System.out.println(x);
	}
}
  •  

    15

    33

    65

    34

  • 15

    32

    65

    34

  •  

    error: interface abstract methods cannot have body


  • 15

    32

    65

    32

  •  

    error: cannot assign a value to final variable x


10. Predict the correct results of the code snippet given below:

class D {
	static int x;
	protected final void getMethod() {
		System.out.println("17");
		++x;
	}
}

public class Main extends D {
	protected final void getMethod() {
		System.out.println("25");
		++x;
	}
	public static void main(String[] args) {
		D object = new D();
		object.getMethod();
		System.out.print(x);
	}
}
  •  

    25

    2

  •  

    Error: getMethod() in Main cannot override getMethod() in D

  •  

    17

    2

  •  

    Error: cannot assign a value to static variable x


11. Predict the results given by below given code snippet.

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class Main {
	public static void main(String[] args) {
		StringBuilder str1 = new StringBuilder("1379");
		System.out.println(str1.toString());
		StringBuilder str2 = str1.reverse();
		System.out.println(str2.toString());
		System.out.println(str1.toString());
	}
}



  •  

    1379

    9731

    1379

  • 1379

    1379

    1379

  • 1379

    9731

    9731

  • Error: cannot find symbol System.out.println(str2.toStrings())

12. Predict the results given by below given code snippet.

enum Laptops {
   MAC(1), LENOVO(MAC), DELL(7), ACER(9);
   private int pricing;
   Laptops(int pricing) {
      this.pricing = pricing;
   }
   public int fetchPrice() {
      return this.pricing;
   }
}
public class Main {
   public static void main(String args[]) {
      Laptops lappy[] = Laptops.values();
      for(Laptops lp: lappy) {
         System.out.println(lp.fetchPrice());
      }
   }
}


  •  

    9

    7

    1

    1

  •  

    Error : illegal start of expression

  •  

    error: incompatible types: Laptops cannot be converted to int


  •  

    1

    1

    7

    9

  •  

    error: cannot find symbol Laptops lappy[] = Laptops.values();


13. Predict the results given by below given code snippet.

enum RandomNumbers {
   RAND1 {
      public String fetchRandom() {
         return "68";
      }
   }, 
   RAND2 {
      public String fetchRandom() {
         return "132";
      }
   }, 
   RAND3 {
     public String fetchRandom() {
         return "297";
      }
   };
   public abstract String fetchRandom();
}


public class Main {
   public static void main(String[] args) {
      for (RandomNumbers rn : RandomNumbers.values()) {
         System.out.println(rn.fetchRandom() + " by " + rn.name());
      }
   }
}
  •  

    297 by RAND3

    132 by RAND2

    68 by RAND1

  •  

    error: cannot find symbol System.out.println(rn.fetchRandom() + " by " + rn.name());


  •  

    132 by RAND2

    297 by RAND3

    68 by RAND1

  •  

    68 by RAND1

    132 by RAND2

    297 by RAND3

  •  

    error: cannot find symbol for (RandomNumbers rn : RandomNumbers.values())


14. Predict the results given by below given code snippet.
public class BigBro {
	public static int a = 69;
	private static int b = 133;
	public static int c = 298;
	private int d = 362;
	
	public static class LittleBro {
		private static int e = 426;
		private static int compute() {
			return (a++ + b + c + d + e++);
		}
	}
	
	public static void main(String[] args) {
		BigBro.LittleBro object = new BigBro.LittleBro();
		System.out.println(object.compute());
	}
	
}
  •  

    1290

  •  

    1289

  •  

    1288

  •  

    error: non-static cannot be referenced from a static context


15. Predict the results given by below given code snippet.
public class BigBro {
	public static int a = 58;
	private static int b = 122;
	public static int c = 286;
	private static int d = 351;
	
	public static class LittleBro {
		private static int e = 415;
		private static int compute() {
			return (a++ + b + c + d + e++);
		}
	}
	
	public static void main(String[] args) {
		BigBro.LittleBro object = new BigBro.LittleBro();
		System.out.println(object.compute());
	}
	
}
  •  

    1234 

  • 1233

  •  

    1232

  • error: non-static cannot be referenced from a static context


16. Predict the results given by below given code snippet.
public class BigBro {
	public static int a = 58;
	private static int b = 122;
	public static int c = 286;
	private static int d = 351;
	
	public static class LittleBro {
		private static int e = 415;
		private static int compute() {
			return (a++ + b + c + d + e++);
		}
	}
	
	public static void main(String[] args) {
		BigBro.LittleBro object = new BigBro.LittleBro();
		System.out.println(object.compute());
	}
	
}
  •  

    1234

  •  

    1233

  •  

    1232

  • error: non-static cannot be referenced from a static context

17. Analyze the below given code and choose the correct option.

public class Main {
	private static int info = 29;
	private static int Abc() {
		class SubMain {
			public int info = 193;
			private int method() {
				return info++;
			}
		};
		SubMain sm = new SubMain();
		return sm.method();
	}
	
	public static void main(String[] args) {
		System.out.println(++info * Abc());
	}
}
  •  

    5820

  •  

    5626

  •  

    5790

  •  

    30194

18. Analyze the below given code and choose the correct option.

final class SubMain {
    void run(){
      System.out.println("117");
      
    }  
}
  
class Main extends SubMain {  
  void run(){
      System.out.println("53");
      
  }  
    
  public static void main(String args[]) {  
  SubMain m= new SubMain();  
  m.run();  
  }  
}  
  • 53

  • 117

    53

  •  

    117

  •  

    error: cannot inherit from final SubMain


19. Analyze the below given code and choose the correct option.

class SubMain {
    void run(){
      System.out.println("121");
      
    }  
}
  
class Main extends SubMain {  
  void run(){
      System.out.println("57");
      
  }  
    
  public static void main(String args[]) {  
  SubMain m= new SubMain();  
  m.run();  
  }  
}  
  •  

    57

  • 121

    57

  •  

    121

  •  

    error: cannot inherit from final SubMain

20. Analyze the below given code and choose the correct option.

interface CodingIndex {
 
    // Defining variables and methods
    int index = 309;
    int getIndex();
}
 
class SubMain implements CodingIndex {
 
    @Override public int getIndex() {
        return(index);
    }
}
 
class Main {
    public static void main(String[] args) {
        SubMain object = new SubMain();
        System.out.print(object.getIndex());
    }
}
  • error: cannot assign a value to final variable index

  •  

    error: cannot find symbol return(index);

  •  

    309

  • Error : Illegal use of @override

21. Analyze the below given code and choose the correct option.

interface CodingIndex {
 
    int index = 120;
    int getIndex();
}
 
class SubMain implements CodingIndex {
    int index = 28;
    public int getIndex() {
        return(++index + index++);
    }
}
 
class Main {
    public static void main(String[] args) {
        SubMain object = new SubMain();
        System.out.print(object.getIndex());
    }
}
  •  

    error: cannot assign a value to final variable index

  •  

    error: cannot find symbol return(index);

  •  

    58

  •  

    242

22. Analyze the below given code and choose the correct option.

public class Main {
	private int methodA(int element) {
		static class SubMain {
			private int methodB() {
				System.out.println("30");
				if(element < 62) {
					return 144;
				}
				else {
					return 176;
				}
			}
		}
		
		SubMain sm = new SubMain();
		return sm.methodB();
	}
	
	public static void main(String[] args) {
		Main m = new Main();
		System.out.println(m.methodA(76));
	}
}
  •  

    error: illegal start of expression static class SubMain

  •  

    30

    176

  •  

    30

    144

  •  

    error: cannot find symbol System.out.println(m.methodA(76));

23. Analyze the below given code and choose the right option.

public class Main {
	private void myMethod() {
		class Inner {
			public int innerMethod() {
				System.out.println("16");
				return 180;
			}
	
		}
		Inner inner = new Inner();
	    System.out.println(inner.innerMethod());
		
	}
	public static void main(String[] args) {
		Main outer = new Main();
	}
}


  •  

    only 16 will be printed

  •  

    only 180 will be printed

  • error: cannot find symbol Inner inner = new Inner();

  •  

    16

    180

  •  

    No error and no output

24. Analyze the below given code and choose the right option.

public class Main {
	private void mainMethod() {
		static int a = 1298;
		class SubMain {
			public int d;
			public int r;
			public SubMain() {
				d = 163;
				r = a % d;
			}
			private int method1() {
				return a % d;
			}
			private int method2()
			{
				System.out.println("2987");
				return a / d;
			}
		}
		
		SubMain sm = new SubMain();
		System.out.println(sm.method1());
		System.out.println(sm.method2());
	}
	
	public static void main(String[] args) {
		Main m = new Main();
		m.mainMethod();
	}
}


  •  

    7

    2987

    157

  •  

    157

    7

    2987

  • 157

    2987

    7

  •  

    error: illegal start of expression

25. Analyze the below given code and choose the right option.

class Universe {
    static int universeA = 13;
	int universeB = 95;
	private static int universeP = 127;
	static class Earth {
		void showTime() {


			System.out.println(universeA);
			System.out.println(universeP);
			System.out.println(universeB);
		
		}
	}
}


public class Main {
	public static void main(String[] args) {
		Universe.Earth object = new Universe.Earth();
		object.showTime();
		
	}
}

  •  

    13

    95

    127

  •  

    error: variable universeB cannot be referenced

  •  

    13

    127

    95

  • 13

    127














Post a Comment

0 Comments