Explore topic-wise MCQs in Engineering.

This section includes 219 Mcqs, each offering curated multiple-choice questions to sharpen your Engineering knowledge and support exam preparation. Choose a topic below to get started.

151.

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
152.

What will be the output of the program?

public class NFE { public static void main(String [] args) { String s = "42"; try { s = s.concat(".5"); /* Line 8 */ double d = Double.parseDouble(s); s = Double.toString(d); int x = (int) Math.ceil(Double.valueOf(s).doubleValue()); System.out.println(x); } catch (NumberFormatException e) { System.out.println("bad number"); } }
}

A. 42
B. 42.5
C. 43
D. bad number
Answer» D. bad number
153.

What will be the output of the program?

interface Count { short counter = 0; void countUp();
}
public class TestCount implements Count { public static void main(String [] args) { TestCount t = new TestCount(); t.countUp(); } public void countUp() { for (int x = 6; x>counter; x--, ++counter) /* Line 14 */ { System.out.print(" " + counter); } }
}

A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. 1 2 3 4
E. Compilation fails
Answer» F.
154.

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
155.

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.
156.

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.
157.

x = 0;
if (x1.hashCode() != x2.hashCode() ) x = x + 1;
if (x3.equals(x4) ) x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
System.out.println("x = " + x);
and assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true?

A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)
Answer» C. x5.hashCode() != x6.hashCode()
158.

Which of the following are true statements?

  1. The Iterator interface declares only three methods: hasNext, next and remove.
  2. The ListIterator interface extends both the List and Iterator interfaces.
  3. The ListIterator interface provides forward and backward iteration capabilities.
  4. The ListIterator interface provides the ability to modify the List during iteration.
  5. The ListIterator interface provides the ability to determine its position in the List.

A. 2, 3, 4 and 5
B. 1, 3, 4 and 5
C. 3, 4 and 5
D. 1, 2 and 3
Answer» C. 3, 4 and 5
159.

Which statement is true for the class java.util.ArrayList?

A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.
Answer» B. The collection is guaranteed to be immutable.
160.

What will be the output of the program?

System.out.println(Math.sqrt(-4D));

A. -2
B. NaN
C. Compile Error
D. Runtime Exception
Answer» C. Compile Error
161.

What will be the output of the program?

String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);

A. apa
B. app
C. apea
D. apep
Answer» C. apea
162.

What will be the output of the program?

public class StringRef { public static void main(String [] args) { String s1 = "abc"; String s2 = "def"; String s3 = s2; /* Line 7 */ s2 = "ghi"; System.out.println(s1 + s2 + s3); }
}

A. abcdefghi
B. abcdefdef
C. abcghidef
D. abcghighi
Answer» D. abcghighi
163.

What will be the output of the program?

public class ExamQuestion7 { static int j; static void methodA(int i) { boolean b; do { b = i<10 | methodB(4); /* Line 9 */ b = i<10 || methodB(8); /* Line 10 */ }while (!b); } static boolean methodB(int i) { j += i; return true; } public static void main(String[] args) { methodA(0); System.out.println( "j = " + j ); } }

A. j = 0
B. j = 4
C. j = 8
D. The code will run with no output
Answer» C. j = 8
164.

What will be the output of the program?

try { Float f1 = new Float("3.0"); int x = f1.intValue(); byte b = f1.byteValue(); double d = f1.doubleValue(); System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{ System.out.println("bad number"); /* Line 11 */
}

A. 9.0
B. bad number
C. Compilation fails on line 9.
D. Compilation fails on line 11.
Answer» B. bad number
165.

What will be the output of the program?

class Q207 { public static void main(String[] args) { int i1 = 5; int i2 = 6; String s1 = "7"; System.out.println(i1 + i2 + s1); /* Line 8 */ } }

A. 18
B. 117
C. 567
D. Compiler error
Answer» C. 567
166.

What will be the output of the program?

public class SqrtExample { public static void main(String [] args) { double value = -9.0; System.out.println( Math.sqrt(value)); }
}

A. 3.0
B. -3.0
C. NaN
D. Compilation fails.
Answer» D. Compilation fails.
167.

What will be the output of the program?

String s = "ABC"; s.toLowerCase(); s += "def"; System.out.println(s);

A. ABC
B. abc
C. ABCdef
D. Compile Error
Answer» D. Compile Error
168.

What will be the output of the program?

public class Test { public static void main(String[] args) { final StringBuffer a = new StringBuffer(); final StringBuffer b = new StringBuffer(); new Thread() { public void run() { System.out.print(a.append("A")); synchronized(b) { System.out.print(b.append("B")); } } }.start(); new Thread() { public void run() { System.out.print(b.append("C")); synchronized(a) { System.out.print(a.append("D")); } } }.start(); } }

A. ACCBAD
B. ABBCAD
C. CDDACB
D. Indeterminate output
Answer» E.
169.

What will be the output of the program?

String s = "hello"; Object o = s; if( o.equals(s) )
{ System.out.println("A"); } else
{ System.out.println("B"); } if( s.equals(o) )
{ System.out.println("C"); } else
{ System.out.println("D"); }
  1. A
  2. B
  3. C
  4. D

A. 1 and 3
B. 2 and 4
C. 3 and 4
D. 1 and 2
Answer» B. 2 and 4
170.

What will be the output of the program (in jdk1.6 or above)?

public class BoolTest { public static void main(String [] args) { Boolean b1 = new Boolean("false"); boolean b2; b2 = b1.booleanValue(); if (!b2) { b2 = true; System.out.print("x "); } if (b1 & b2) /* Line 13 */ { System.out.print("y "); } System.out.println("z"); }
}

A. z
B. x z
C. <!--<p> Answer:The compiler fails at line 13 because b1 is a reference variable to a <i class="java-code">Boolean</i> wrapper object, not a boolean primitive. Logical boolean tests can't be made on <i class="java-code">Boolean</i> objects.</p> -->
D. y z
E. Compilation fails.
Answer» C. <!--<p> Answer:The compiler fails at line 13 because b1 is a reference variable to a <i class="java-code">Boolean</i> wrapper object, not a boolean primitive. Logical boolean tests can't be made on <i class="java-code">Boolean</i> objects.</p> -->
171.

What will be the output of the program?

public class Test138 { public static void stringReplace (String text) { text = text.replace ('j' , 'c'); /* Line 5 */ } public static void bufferReplace (StringBuffer text) { text = text.append ("c"); /* Line 9 */ } public static void main (String args[]) { String textString = new String ("java"); StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */ stringReplace(textString); bufferReplace(textBuffer); System.out.println (textString + textBuffer); } }

A. java
B. javac
C. javajavac
D. Compile error
Answer» D. Compile error
172.

What will be the output of the program?

class Tree { } class Pine extends Tree { } class Oak extends Tree { } public class Forest1 { public static void main (String [] args) { Tree tree = new Pine(); if( tree instanceof Pine ) System.out.println ("Pine"); else if( tree instanceof Tree ) System.out.println ("Tree"); else if( tree instanceof Oak ) System.out.println ( "Oak" ); else System.out.println ("Oops "); } }

A. Pine
B. Tree
C. Forest
D. Oops
Answer» B. Tree
173.

What will be the output of the program?

String d = "bookkeeper";
d.substring(1,7);
d = "w" + d;
d.append("woo"); /* Line 4 */
System.out.println(d);

A. wookkeewoo
B. wbookkeeper
C. wbookkeewoo
D. Compilation fails.
Answer» E.
174.

What will be the output of the program?


String a = "ABCD"; String b = a.toLowerCase(); b.replace('a','d'); b.replace('b','c'); System.out.println(b);

A. abcd
B. ABCD
C. dccd
D. dcba
Answer» B. ABCD
175.

What will be the output of the program?

public class ExamQuestion6 { static int x; boolean catch() { x++; return true; } public static void main(String[] args) { x=0; if ((catch() | catch()) || catch()) x++; System.out.println(x); } }

A. 1
B. 2
C. 3
D. Compilation Fails
Answer» E.
176.

What will be the output of the program?

public class Test178 { public static void main(String[] args) { String s = "foo"; Object o = (Object)s; if (s.equals(o)) { System.out.print("AAA"); } else { System.out.print("BBB"); } if (o.equals(s)) { System.out.print("CCC"); } else { System.out.print("DDD"); } } }

A. AAACCC
B. AAADDD
C. BBBCCC
D. BBBDDD
Answer» B. AAADDD
177.

What will be the output of the program?

String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);

A. abcXyZ
B. abcxyz
C. xyzabc
D. XyZabc
Answer» D. XyZabc
178.

What will be the output of the program?

int i = (int) Math.random();

A. i = 0
B. i = 1
C. value of i is undetermined
D. Statement causes a compile error
Answer» B. i = 1
179.

What will be the output of the program?

class A { public A(int x){} } class B extends A { } public class test { public static void main (String args []) { A a = new B(); System.out.println("complete"); } }

A. It compiles and runs printing nothing
B. Compiles but fails at runtime
C. Compile Error
D. Prints "complete"
Answer» D. Prints "complete"
180.

What will be the output of the program?

int i = 1, j = 10; do { if(i++ > --j) /* Line 4 */ { continue; } } while (i < 5); System.out.println("i = " + i + "and j = " + j); /* Line 9 */

A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 6
D. i = 5 and j = 6
Answer» E.
181.

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 2
B. 2 and 3
C. 3 and 4
D. 1 and 3
Answer» D. 1 and 3
182.

Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?

  1. If the equals() method returns true, the hashCode() comparison == must return true.
  2. If the equals() method returns false, the hashCode() comparison != must return true.
  3. If the hashCode() comparison == returns true, the equals() method must return true.
  4. If the hashCode() comparison == returns true, the equals() method might return true.

A. 1 and 4
B. 2 and 3
C. 3 and 4
D. 1 and 3
Answer» B. 2 and 3
183.

Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?

A. java.util.ArrayList
B. java.util.LinkedHashMap
C. java.util.HashMap
D. java.util.TreeMap
Answer» C. java.util.HashMap
184.

Which interface provides the capability to store objects using a key-value pair?

A. Java.util.Map
B. Java.util.Set
C. Java.util.List
D. Java.util.Collection
Answer» B. Java.util.Set
185.

Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?

A. java.util.SortedMap
B. java.util.TreeMap
C. java.util.TreeSet
D. java.util.Hashtable
Answer» E.
186.

Which is valid declaration of a float?

A. float f = 1F;
B. float f = 1.0;
C. float f = "1";
D. float f = 1.0d;
Answer» B. float f = 1.0;
187.

/* 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"); } }
What line of code should replace the missing statement to make this program compile?

A. No statement required.
B. import java.io.*;
C. include java.io.*;
D. import java.io.PrintWriter;
Answer» B. import java.io.*;
188.

What is the numerical range of char?

A. 0 to 32767
B. 0 to 65535
C. -256 to 255
D. -32768 to 32767
Answer» C. -256 to 255
189.

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
190.

interface DoMath { double getArea(int rad); }
interface MathPlus { double getVol(int b, int h); }
/* Missing Statements ? */
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; } }

A. 1 only
B. 2 only
C. 3 and 5
D. 1 and 4
Answer» D. 1 and 4
191.

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().
  6. 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.
192.

/* 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(); }
}
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;

A. 1 only
B. 2 and 5
C. 3 and 4
D. 3 and 5
Answer» C. 3 and 4
193.

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
194.


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 */ }
}
which statement is true?

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.
195.

Which three are valid method signatures in an interface?

  1. private int getArea();
  2. public float getVol(float x);
  3. public void main(String [] args);
  4. public static void main(String [] args);
  5. boolean setFlag(Boolean [] test);

A. 1 and 2
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4
Answer» C. 3, 4, and 5
196.

What will be the output of the program?

String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects have been created?

A. 2
B. 3
C. 4
D. 5
Answer» D. 5
197.

What will be the output of the program?

public class WrapTest { public static void main(String [] args) { int result = 0; short s = 42; Long x = new Long("42"); Long y = new Long(42); Short z = new Short("42"); Short x2 = new Short(s); Integer y2 = new Integer("42"); Integer z2 = new Integer(42); if (x == y) /* Line 13 */ result = 1; if (x.equals(y) ) /* Line 15 */ result = result + 10; if (x.equals(z) ) /* Line 17 */ result = result + 100; if (x.equals(x2) ) /* Line 19 */ result = result + 1000; if (x.equals(z2) ) /* Line 21 */ result = result + 10000; System.out.println("result = " + result); }
}

A. result = 1
B. result = 10
C. result = 11
D. result = 11010
Answer» C. result = 11
198.

What will be the output of the program?

public class BoolTest { public static void main(String [] args) { int result = 0; Boolean b1 = new Boolean("TRUE"); Boolean b2 = new Boolean("true"); Boolean b3 = new Boolean("tRuE"); Boolean b4 = new Boolean("false"); if (b1 == b2) /* Line 10 */ result = 1; if (b1.equals(b2) ) /* Line 12 */ result = result + 10; if (b2 == b4) /* Line 14 */ result = result + 100; if (b2.equals(b4) ) /* Line 16 */ result = result + 1000; if (b2.equals(b3) ) /* Line 18 */ result = result + 10000; System.out.println("result = " + result); }
}

A. 0
B. 1
C. 10
D. 10010
Answer» E.
199.

What will be the output of the program?

public class ObjComp { public static void main(String [] args ) { int result = 0; ObjComp oc = new ObjComp(); Object o = oc; if (o == oc) result = 1; if (o != oc) result = result + 10; if (o.equals(oc) ) result = result + 100; if (oc.equals(o) ) result = result + 1000; System.out.println("result = " + result); }
}

A. 1
B. 10
C. 101
D. 1101
Answer» E.
200.

What will be the output of the program?

public class Example { public static void main(String [] args) { double values[] = {-2.3, -1.0, 0.25, 4}; int cnt = 0; for (int x=0; x < values.length; x++) { if (Math.round(values[x] + .5) == Math.ceil(values[x])) { ++cnt; } } System.out.println("same results " + cnt + " time(s)"); }
}

A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.
Answer» C. same results 4 time(s)