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()); } }
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(); } }
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()); } } }
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); } }
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)); } }
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(); } }
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(); } }
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(); } }
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); } }
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); } }
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()); } }
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()); } } }
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()); } } }
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()); } }
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()); } }
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()); } }
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()); } }
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(); } }
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(); } }
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()); } }
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()); } }
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)); } }
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(); } }
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(); } }
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(); } }
0 Comments