Posts which you will also like.

Saturday, June 23, 2012

Java Multiple Choice Questions(MCQ)


This post contains 20 multiple choice question (mcq) in java.Try to solve them for your practice......

1.Given: public class Mule { public static void main(String[] args) { boolean assert = true; if(assert) { System.out.println("assert is true"); } } } Which command-line invocations will compile? 
a.javac Mule.java
b.javac -source 1.3 Mule.java
c.javac -source 1.4 Mule.java
d.javac -source 1.5 Mule.java
2) Which exception is thrown by read() method of InputStream class
a.Exception
b.FileNotFoundException
c.IOException
d.None
3)Given: String test = "a1b2c3"; String[] tokens = test.split("\\d"); for(String s: tokens) System.out.print(s + " "); What is the result?
a.a b c
b.1 2 3
c.a1b2c3
d.a1 b2 c3
4)Given: public class Person { private name; public Person(String name) { this.name = name; } public int hashCode() { return 420; } } Which statement is true?
a.The time to find the value from HashMap with a Person key depends on the size of the map.
b.Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
c.Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
d.The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.
5)Given: abstract class C1 { public C1() { System.out.print(1); } } class C2 extends C1 { public C2() { System.out.print(2); } } class C3 extends C2 { public C3() { System.out.println(3); } } public class Ctest { public static void main(String[] a) { new C3(); } } What is the result?
a.3
b.23
c.32
d.123
6)Given: class One { public One foo() { return this; } } class Two extends One { public One foo() { return this; } } class Three extends Two { // insert method here } Which methods, inserted individually, correctly complete the Three class?
a.public void foo() {}
b.public int foo() { return 3; }
c.public Two foo() { return this; }
d.public Object foo() { return this; }
7)Which statements is true?
a.A final method in class X can be abstract if and only if X is abstract.
b.A protected method in class X can be overridden by any subclass of X.
c.A private static method can be called only within other static methods in class X.
d.A non-static public final method in class X can be overridden in any subclass of X.
8)Given: public interface Status { /* insert code here */ int MY_VALUE = 10; } Which three are valid on line
a.final
b.native
c.private
d.abstract
9)A team of programmers is involved in reviewing a proposed design for a new utility class. After some discussion, they realize that the current design allows other classes to access methods in the utility class that should be accessible only to methods within the utility class itself. What design issue has the team discovered?
a.Weak encapsulation
b.Strong encapsulation
c.High cohesion
d.Tight coupling
10)Which of the following strings can be used as mode strings for creating a RandomAccessFile object? 
a.“r”
b.“w”
c.“wr”
d.“0”
11)Given: public class TestOne { public static void main (String[] args) throws Exception { Thread.sleep(3000); System.out.println("sleep"); } } What is the result?
a.Compilation fails.
b.An exception is thrown at runtime.
c.The code executes normally and prints "sleep".
d.The code executes normally, but nothing is printed.
12)Given: class Mammal { } class Raccoon extends Mammal { Mammal m = new Mammal(); } class BabyRaccoon extends Mammal { } Which four statements are true?
a.Raccoon is-a Mammal.
b.BabyRaccoon has-a Mammal.
c.BabyRaccoon is-a Raccoon.
d.BabyRaccoon is-a BabyRaccoon.
13)Which of the following lines will not compile? byte b1=5, b2=3, b3; Short s=25; b2=s; b3=b1*b2;
a.Line 1only
b.Line 3 only
c.Line 4 only
d.Line 1 and 4 only
14)Given: class Nav{ public enum Direction { NORTH, SOUTH, EAST, WEST } } public class Sprite{ // insert code here } Which code,at "inserted code here", allows the Sprite class to compile?
a.Direction d = NORTH;
b.Nav.Direction d = NORTH;
c.Direction d = Direction.NORTH;
d.Nav.Direction d = Nav.Direction.NORTH;
15)Given: public static void main(String[] args) { Object obj = new int[] { 1, 2, 3 }; int[] someArray = (int[])obj; for (int i : someArray) System.out.print(i + " "); } What is the result?
a.1 2 3
b.Compilation fails because of an error
c.A ClassCastException is thrown at runtime.
d.none
16)Given: import java.util.*; public class SortOf { public static void main(String[] args) { public static void main(String[] args) { a.add(1); a.add(5); a.add(3); Collections.sort(a); a.add(2); Collections.reverse(a); System.out.println(a); } } What is the result?
a.[1, 2, 3, 5]
b.[2, 1, 3, 5]
c.[2, 5, 3, 1]
d.[5, 3, 2, 1]
17)Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?
a.When using versions of Java technology earlier than 5.0.
b.When sharing a StringBuffer among multiple threads.
c.When using the java.io class StringBufferInputStream.
d.When you plan to reuse the StringBuffer to build more than one string.
18)Which of the following statements are true?
a.Unicode characters are all 16 bits
b.UTF characters are all 24 bits
c.Reader class is byte stream class
d.None
19)Which statements are reserved words in Java?
a.run
b.imports
c.default
d.implement
20)DataInputStream is
a.An abstract class defined in java.io
b.A class we can use to read primitive data type
c.An interface that defines methods to open files
d.An interface that defines methods to read primitive data types


For more such mcq please visit our partner techcresendo.com

1 comment:

  1. If u can upload answer sheet at last, then it would certainly help to find mistakes.

    ReplyDelete

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool