

MCQOPTIONS
Saved Bookmarks
This section includes 1698 Mcqs, each offering curated multiple-choice questions to sharpen your General Awareness knowledge and support exam preparation. Choose a topic below to get started.
551. |
What will be the output of the program? class Super { public int i = 0; public Super(String text) /* Line 4 */ { i = 1; } } class Sub extends Super { public Sub(String text) { i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } } |
A. | 0 |
B. | 1 |
C. | 2 |
D. | Compilation fails. |
Answer» E. | |
552. |
What will be the output of the program? class Base { Base() { System.out.print("Base"); } } public class Alpha extends Base { public static void main(String[] args) { new Alpha(); /* Line 12 */ new Base(); /* Line 13 */ } } |
A. | Base |
B. | BaseBase |
C. | Compilation fails |
D. | The code runs with no output |
Answer» C. Compilation fails | |
553. |
What will be the output of the program? import java.util.*; public class NewTreeSet2 extends NewTreeSet { public static void main(String [] args) { NewTreeSet2 t = new NewTreeSet2(); t.count(); } } protected class NewTreeSet { void count() { for (int x = 0; x < 7; x++,x++ ) { System.out.print(" " + x); } } } |
A. | 0 2 4 |
B. | 0 2 4 6 |
C. | Compilation fails at line 2 |
D. | Compilation fails at line 10 |
Answer» E. | |
554. |
What will be the output of the program? public class ArrayTest { public static void main(String[ ] args) { float f1[ ], f2[ ]; f1 = new float[10]; f2 = f1; System.out.println("f2[0] = " + f2[0]); } } |
A. | It prints f2[0] = 0.0 |
B. | It prints f2[0] = NaN |
C. | An error at f2 = f1; causes compile to fail. |
D. | It prints the garbage value. |
Answer» B. It prints f2[0] = NaN | |
555. |
What will be the output of the program? class Super { public Integer getLength() { return new Integer(4); } } public class Sub extends Super { public Long getLength() { return new Long(5); } public static void main(String[] args) { Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getLength().toString() + "," + sub.getLength().toString() ); } } |
A. | 4, 4 |
B. | 4, 5 |
C. | 5, 4 |
D. | Compilation fails. |
Answer» E. | |
556. |
What will be the output of the program? class A { final public int GetResult(int a, int b) { return 0; } } class B extends A { public int GetResult(int a, int b) {return 1; } } public class Test { public static void main(String args[]) { B b = new B(); System.out.println("x = " + b.GetResult(0, 1)); } } |
A. | X = 0 |
B. | X = 1 |
C. | Compilation fails. |
D. | An exception is thrown at runtime. |
Answer» D. An exception is thrown at runtime. | |
557. |
What will be the output of the program? public class Test { public static void main(String args[]) { class Foo { public int i = 3; } Object o = (Object)new Foo(); Foo foo = (Foo)o; System.out.println("i = " + foo.i); } } |
A. | I = 3 |
B. | Compilation fails. |
C. | I = 5 |
D. | A ClassCastException will occur. |
Answer» B. Compilation fails. | |
558. |
Which two code fragments inserted at end of the program, will allow to compile? 1. class AllMath extends DoMath { double getArea(int r); } 2. interface AllMath implements MathPlus { double getVol(int x, int y); } 3. interface AllMath extends DoMath { float getAvg(int h, int l); } 4. class AllMath implements MathPlus { double getArea(int rad); } 5. abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } } interface DoMath { double getArea(int rad); } interface MathPlus { double getVol(int b, int h); } /* Missing Statements ? */ |
A. | 1 only |
B. | 2 only |
C. | 3 and 5 |
D. | 1 and 4 |
Answer» D. 1 and 4 | |
559. |
What will be the output of the program? public class Test { public int aMethod() { static int i = 0; i++; return i; } public static void main(String args[]) { Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } } |
A. | 0 |
B. | 1 |
C. | 2 |
D. | Compilation fails. |
Answer» E. | |
560. |
What will be the output of the program? public class A { void A() /* Line 3 */ { System.out.println("Class A"); } public static void main(String[] args) { new A(); } } |
A. | Class A |
B. | Compilation fails. |
C. | An exception is thrown at line 3. |
D. | The code executes with no output. |
Answer» E. | |
561. |
Which two statements, added independently at beginning of the program, allow the code to compile? 1. No statement is required 2. import java.util.*; 3. import.java.util.Tree*; 4. import java.util.TreeSet; 5. import java.util.TreeMap; /* Missing statements ? */ public class NewTreeSet extends java.util.TreeSet { public static void main(String [] args) { java.util.TreeSet t = new java.util.TreeSet(); t.clear(); } public void clear() { TreeMap m = new TreeMap(); m.clear(); } } |
A. | 1 only |
B. | 2 and 5 |
C. | 3 and 4 |
D. | 3 and 5 |
Answer» C. 3 and 4 | |
562. |
Which two statements are true for any concrete class implementing the java.lang.Runnable interface? 1. You can extend the Runnable interface as long as you override the public run() method. 2. The class must contain a method called run() from which all code for that thread will be initiated. 3. The class must contain an empty public void method named run(). 4. The class must contain a public void method named runnable(). 5. The class definition must include the words implements Threads and contain a method called run(). The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments. |
A. | 1 and 3 |
B. | 2 and 4 |
C. | 1 and 5 |
D. | 2 and 6 |
Answer» E. | |
563. |
Which three statements are true? 1. The default constructor initialises method variables. 2. The default constructor has the same access as its class. 3. The default constructor invokes the no-arg constructor of the superclass. 4. If a class lacks a no-arg constructor, the compiler always creates a default constructor. 5. The compiler creates a default constructor only when there are no other constructors for the class. |
A. | 1, 2 and 4 |
B. | 2, 3 and 5 |
C. | 3, 4 and 5 |
D. | 1, 2 and 3 |
Answer» C. 3, 4 and 5 | |
564. |
Which of the following are Java reserved words? 1. run 2. import 3. default 4. implement |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 2 and 4 |
Answer» C. 3 and 4 | |
565. |
Which statement is true? package testpkg.p1; public class ParentUtil { public int x = 420; protected int doStuff() { return x; } } package testpkg.p2; import testpkg.p1.ParentUtil; public class ChildUtil extends ParentUtil { public static void main(String [] args) { new ChildUtil().callStuff(); } void callStuff() { System.out.print("this " + this.doStuff() ); /* Line 18 */ ParentUtil p = new ParentUtil(); System.out.print(" parent " + p.doStuff() ); /* Line 20 */ } } |
A. | The code compiles and runs, with output this 420 parent 420. |
B. | If line 18 is removed, the code will compile and run. |
C. | If line 20 is removed, the code will compile and run. |
D. | An exception is thrown at runtime. |
Answer» D. An exception is thrown at runtime. | |
566. |
What line of code should replace the missing statement to make this program compile? /* Missing Statement ? */ public class foo { public static void main(String[]args)throws Exception { java.io.PrintWriter out = new java.io.PrintWriter(); new java.io.OutputStreamWriter(System.out,true); out.println("Hello"); } } |
A. | No statement required. |
B. | Import java.io.*; |
C. | Include java.io.*; |
D. | Import java.io.PrintWriter; |
Answer» B. Import java.io.*; | |
567. |
What will be the output of the program? public class Test { private static float[] f = new float[2]; public static void main (String[] args) { System.out.println("f[0] = " + f[0]); } } |
A. | F[0] = 0 |
B. | F[0] = 0.0 |
C. | Compile Error |
D. | Runtime Exception |
Answer» C. Compile Error | |
568. |
What will be the output of the program? import java.util.*; class H { public static void main (String[] args) { Object x = new Vector().elements(); System.out.print((x instanceof Enumeration)+","); System.out.print((x instanceof Iterator)+","); System.out.print(x instanceof ListIterator); } } |
A. | Prints: false,false,false |
B. | Prints: false,false,true |
C. | Prints: false,true,false |
D. | Prints: true,false,false |
Answer» E. | |
569. |
What will be the output of the program? TreeSet map = new TreeSet(); map.add("one"); map.add("two"); map.add("three"); map.add("four"); map.add("one"); Iterator it = map.iterator(); while (it.hasNext() ) { System.out.print( it.next() + " " ); } |
A. | One two three four |
B. | Four three two one |
C. | Four one three two |
D. | One two three four one |
Answer» D. One two three four one | |
570. |
Which statement is true? class Test1 { public int value; public int hashCode() { return 42; } } class Test2 { public int value; public int hashcode() { return (int)(value^5); } } |
A. | Class Test1 will not compile. |
B. | The Test1 hashCode() method is more efficient than the Test2 hashCode() method. |
C. | The Test1 hashCode() method is less efficient than the Test2 hashCode() method. |
D. | Class Test2 will not compile. |
Answer» D. Class Test2 will not compile. | |
571. |
What is the output of this program? interface calculate { int VAR = 0; void cal(int item); } class display implements calculate { int x; public void cal(int item) { if (item<2) x = VAR; else x = item * item; } } class interfaces { public static void main(String args[]) { display[] arr=new display[3]; for(int i=0;i<3;i++) arr[i]=new display(); arr[0].cal(0); arr[1].cal(1); arr[2].cal(2); System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x); } } |
A. | 0 1 2 |
B. | 0 2 4 |
C. | 0 0 4 |
D. | 0 1 4 |
Answer» D. 0 1 4 | |
572. |
Which of the following statements about the hashcode() method are incorrect? 1. The value returned by hashcode() is used in some collection classes to help locate objects. 2. The hashcode() method is required to return a positive int value. 3. The hashcode() method in the String class is the one inherited from Object. 4. Two new empty String objects will produce identical hashcodes. |
A. | 1 and 2 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 4 |
Answer» C. 3 and 4 | |
573. |
What two statements are true about properly overridden hashCode() and equals() methods? 1. hashCode() doesn t have to be overridden if equals() is. 2. equals() doesn t have to be overridden if hashCode() is. 3. hashCode() can always return the same value, regardless of the object that invoked it. 4. equals() can be true even if it s comparing different objects. |
A. | 1 and 4 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 1 and 3 |
Answer» B. 2 and 3 | |
574. |
Which of these is an incorrect array declaration? |
A. | Int arr[] = new int[5]. |
B. | Int [] arr = new int[5]. |
C. | Int arr[] = new int[5]. |
D. | Int arr[] = int [5] new |
Answer» E. | |
575. |
What is the output of this program? class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } } } |
A. | 0 2 4 6 8 |
B. | 1 3 5 7 9 |
C. | 0 1 2 3 4 5 6 7 8 9 |
D. | 1 2 3 4 5 6 7 8 9 10 |
Answer» B. 1 3 5 7 9 | |
576. |
What is the output of this program? class multidimention_array { public static void main(String args[]) { int arr[][] = new int[3][]; arr[0] = new int[1]; arr[1] = new int[2]; arr[2] = new int[3]; int sum = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < i + 1; ++j) arr[i][j] = j + 1; for (int i = 0; i < 3; ++i) for (int j = 0; j < i + 1; ++j) sum + = arr[i][j]; System.out.print(sum); } } |
A. | 11 |
B. | 10 |
C. | 13 |
D. | 14 |
Answer» C. 13 | |
577. |
What will this code print? int arr[] = new int [5]; System.out.print(arr); |
A. | 0 |
B. | Value stored in arr[0]. |
C. | 00000 |
D. | Garbage value |
Answer» E. | |
578. |
What is the output of below snippet? Object[] names = new String[3]; names[0] = new Integer(0); |
A. | ArrayIndexOutOfBoundsException |
B. | ArrayStoreException |
C. | Compilation Error |
D. | Code runs successfully |
Answer» C. Compilation Error | |
579. |
What is the correct option with regards to below snippet? interface ICust { } class RegularCustomer implements ICust { } class OneTimeCustomer implements ICust { } |
A. | ICust can be replaced with RegularCustomer |
B. | RegularCustomer can be replaced with OneTimeCustomer |
C. | OneTimeCustomer can be replaced with RegularCustomer |
D. | We can instantiate objects of ICust |
Answer» B. RegularCustomer can be replaced with OneTimeCustomer | |
580. |
What is the outcome of below Main class? public class Shape { public int area() { return 1; } } public class Square extends Shape { public int area() { return 2; } } class Main() { public static void main(String[] args) { Shape shape = new Shape(); Square square = new Square(); shape = square; System.out.println(shape.area()); } } |
A. | Compilation failure |
B. | Runtime failure |
C. | 1 |
D. | 2 |
Answer» E. | |
581. |
What is the outcome of below code Main class? public class Shape { public int area() { return 1; } } public class Rectangle extends Shape { public int area() { return 3; } } class Main() { public static void main(String[] args) { Shape shape = new Shape(); Rectangle rect = new Rectangle(); shape = rect; System.out.println(shape.area()); } } |
A. | Compilation failure |
B. | 3 |
C. | 1 |
D. | 2 |
Answer» C. 1 | |
582. |
What is the outcome of below code Main class? public class Shape { public int area() { return 1; } } public class Square extends Shape { public int area() { return 2; } } class Main() { public static void main(String[] args) { Shape shape = new Shape(); Square square = new Square(); square = shape; System.out.println(square.area()); } } |
A. | Compilation failure |
B. | 3 |
C. | 1 |
D. | 2 |
Answer» B. 3 | |
583. |
What is the outcome of below code Main class? public class Shape { public int area() { return 1; } } public class Square extends Shape { public int area() { return 2; } } public class Rectangle extends Shape { public int area() { return 3; } } class Main() { public static void main(String[] args) { Shape shape = new Shape(); Square square = new Square(); Rectangle rect = new Rectangle(); rect = (Rectangle)shape; System.out.println(square.area()); } } |
A. | Compilation failure |
B. | 3 |
C. | Runtime Exception |
D. | 2 |
Answer» B. 3 | |
584. |
What is the outcome of below code Main class? public class Shape { public int area() { return 1; } } public class Square extends Shape { public int area() { return 2; } } public class Rectangle extends Shape { public int area() { return 3; } } class Main() { public static void main(String[] args) { Shape shape = new Shape(); Square square = new Square(); Rectangle rect = new Rectangle(); rect = (Rectangle)square; System.out.println(square.area()); } } |
A. | Compilation failure |
B. | 3 |
C. | Runtime Exception |
D. | 2 |
Answer» C. Runtime Exception | |
585. |
What is the outcome of below code Main class? public class Shape { public int area() { return 1; } } public class Square extends Shape { public int area() { return 2; } } public class Rectangle extends Shape { public int area() { return 3; } } public static void main(String[] args) { Shape shape = new Square(); shape = new Rectangle(); System.out.println(shape.area()); } |
A. | Compilation failure |
B. | 3 |
C. | Runtime Exception |
D. | 2 |
Answer» C. Runtime Exception | |
586. |
What is the output of this program? import java.util.*; class Output { public static void addNumbers(List super Integer> list) { for (int i = 1; i <= 10; i++) { list.add(i); } } public static void main(String args[]) { List ld = Arrays.asList(); addnumbers(10.4); System.out.println("getList(2)"); } } |
A. | 1 |
B. | 2 |
C. | 3 |
D. | 6 |
Answer» B. 2 | |
587. |
What will be the output of the program? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) ? "assertion failed" : "assertion passed" ; System.out.println("finished"); } } |
A. | Finished |
B. | Compiliation fails. |
C. | An AssertionError is thrown and finished is output. |
D. | An AssertionError is thrown with the message "assertion failed." |
Answer» C. An AssertionError is thrown and finished is output. | |
588. |
What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } } |
A. | Bar |
B. | Bar done |
C. | Foo done |
D. | Compilation fails |
Answer» E. | |
589. |
What causes compilation to fail? public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } |
A. | Line 5 |
B. | Line 6 |
C. | Line 12 |
D. | Line 14 |
Answer» E. | |
590. |
What will be the output of the program (when you run with the -ea option) ? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } } |
A. | Finished |
B. | Compilation fails. |
C. | An AssertionError is thrown. |
D. | An AssertionError is thrown and finished is output. |
Answer» D. An AssertionError is thrown and finished is output. | |
591. |
Which line is an example of an inappropriate use of assertions? public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } } |
A. | Line 11 |
B. | Line 12 |
C. | Line 14 |
D. | Line 22 |
Answer» E. | |
592. |
Which three statements are true? 1. Assertion checking is typically enabled when a program is deployed. 2. It is never appropriate to write code to handle failure of an assert statement. 3. Assertion checking is typically enabled during program development and testing. 4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis. 5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis. |
A. | 1, 2 and 4 |
B. | 2, 3 and 5 |
C. | 3, 4 and 5 |
D. | 1, 2 and 5 |
Answer» C. 3, 4 and 5 | |
593. |
What causes compilation to fail? public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } }public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } |
A. | Line 5 |
B. | Line 6 |
C. | Line 12 |
D. | Line 14 |
Answer» E. | |
594. |
What is the output of this program? Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time). import java.text.*; import java.util.*; class Date_formatting { public static void main(String args[]) { Date date = new Date(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("mm:hh:ss"); System.out.print(sdf.format(date)); } } |
A. | 3:55:4 |
B. | 3.55.4 |
C. | 55:03:04 |
D. | 03:55:04 |
Answer» D. 03:55:04 | |
595. |
What is the output of this program? Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time). import java.text.*; import java.util.*; class Date_formatting { public static void main(String args[]) { Date date = new Date(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("hh:mm:ss"); System.out.print(sdf.format(date)); } } |
A. | 3:55:4 |
B. | 3.55.4 |
C. | 55:03:04 |
D. | 03:55:04 |
Answer» E. | |
596. |
What is the output of this program? Note: The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time). import java.text.*; import java.util.*; class Date_formatting { public static void main(String args[]) { Date date = new Date(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("E MMM dd yyyy"); System.out.print(sdf.format(date)); } } |
A. | Mon Jul 15 2013 |
B. | Jul 15 2013 |
C. | 55:03:04 Mon Jul 15 2013 |
D. | 03:55:04 Jul 15 2013 |
Answer» B. Jul 15 2013 | |
597. |
Note : The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time). import java.text.*; import java.util.*; class Date_formatting { public static void main(String args[]) { Date date = new Date(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("z"); System.out.print(sdf.format(date)); } } |
A. | Z |
B. | Jul |
C. | Mon |
D. | PDT |
Answer» E. | |
598. |
What is the output of this program? import java.util.*; class Output { public static double sumOfList(List extends Number> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s; } public static void main(String args[]) { List li = Arrays.asList(1, 2, 3); System.out.println(sumOfList(li)); } } |
A. | 0 |
B. | 4 |
C. | 5.0 |
D. | 6.0 |
Answer» E. | |
599. |
What is the output of this program? import java.util.*; class Output { public static double sumOfList(List extends Number> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s; } public static void main(String args[]) { List ld = Arrays.asList(1.2, 2.3, 3.5); System.out.println(sumOfList(ld)); } } |
A. | 5.0 |
B. | 7.0 |
C. | 8.0 |
D. | 6.0 |
Answer» C. 8.0 | |
600. |
What is the output of this program? import java.util.*; public class genericstack { Stack stk = new Stack (); public void push(E obj) { stk.push(obj); } public E pop() { E obj = stk.pop(); return obj; } } class Output { public static void main(String args[]) { genericstack gs = new genericstack(); gs.push("Hello"); System.out.println(gs.pop()); } } |
A. | H |
B. | Hello |
C. | Runtime Error |
D. | Compilation Error |
Answer» C. Runtime Error | |