Java MCQs

Java MCQs

Try to answer these 800+ Java MCQs and check your understanding of the Java subject. Scroll down and let's begin!

1: A function that defines the steps necessary to instantiate one object of that class is called:

A.   an instantiator.

B.   a constructor.

C.   a destructor.

2: Will a program compile if the main method is defined private?

A.   No, it does not compile

B.   Yes, but will not run

3: How many objects are created: String s1="String"; String s2="String"; String s3="String";

A.   Two

B.   Three

C.   One

4: How do we compare enum types in java

A.   Arithmetic comparator "=="

B.   Equals Method

C.   Both

5: If you need to determine exactly which class your object is:

A.   use the myClass property.

B.   use the instanceOf operator.

C.   use the getClass() method.

6: Which java class provides variables local to each thread?

A.   LocalThread

B.   Thread

C.   Runnable

D.   ThreadLocal

7: Which method is used to force one thread to wait for another thread to finish?

A.   stop()

B.   sleep(long milliseconds)

C.   yield()

D.   join()

8: What does the synchronized keyword on a method do?

A.   Ensures that each call to a synchronized method is run in a separate thread.

B.   Creates a new semaphore to prevent two threads from accessing the method simultaneously.

C.   Prevents objects outside the current package from accessing the method.

D.   Uses the object's intrinsic lock to prevent two threads from accessing the method simultaneously.

9: Which of the following is true for interface variables.

A.   They may exist, and they can be transient but not volatile.

B.   They cannot exist and the compiler will throw a 'field name is ambiguous' error if you attempt to make them.

C.   They may exist, and they can be transient and volatile.

D.   They may exist, and they can be volatile but not transient.

E.   They may exist, but they must be public, static and final.

10: The @Override annotation

A.   All of these answers are correct

B.   Is not required to implement an interface method.

C.   Is not required to override an inherited method in a parent class.

D.   Will warn you on compilation if your annotated method signature doesn't match a method in a parent class or interface.

E.   Clearly documents the intention to override a method in a parent class or implement a method declared in an interface.

11: The StringBuffer and StringBuilder classes in Java are optimized for

A.   creating strings which change considerably.

B.   creating strings which only change once.

C.   retrieval of all or part of a string but not altering the string.

12: A deadlock error occurs when a Java program

A.   has a circular dependency on 2 or more synchronized objects, causing one of the objects to wait indefinitely.

B.   has a for loop or while loop that can never satisfy its condition, causing the loop to run forever and the program to wait indefinitely.

C.   has a circular try-catch block, in which the catch results in the catch block being reached again repeatedly, causing the program to run indefinitely.

13: What will be the output of below code String a="abc"; String b="abc"; String c=new String("abc"); if(a==c && a.equals(b)){ System.out.println("They are equal"); } else System.out.println("They are not equal");

A.   They are not equal

B.   They are equal

14: public class A { private int m = 5; class N { private int m = 3; public void print() { System.out.println("m=" + m); } } public static void main(String[] args) { A a = new A(); N n = N(); n.print(); } } What is the output?

A.   m=5

B.   m=3

C.   Runtime error

D.   Compile error

15: Can static and abstract keywords be used together?

A.   No

B.   Yes

16: You read the following statement in a valid Java program: submarine.dive(depth); What must be true?

A.   "depth" must be an int.

B.   "submarine" must be a method.

C.   "dive" must be a method.

D.   "submarine" must be the name of a class.

E.   "dive" must be the name of an instance field.

17: What's wrong with the following method: public static int getSize(){ int temp = super.getSize(); if(temp==0) temp=this.size; return temp; }

A.   There is nothing wrong with this method.

B.   static methods cannot return type "int"

C.   static methods cannot refer to "this" or "super"

18: Which statement is wrong?

A.   ArrayList<String> myList = new ArrayList<String>(10);

B.   ArrayList<String> myList = new ArrayList<String>();

C.   ArrayList<Integer> myList = new ArrayList<Integer>();

D.   ArrayList<Double> myList = new ArrayList<Double>();

E.   ArrayList<String> myList = new ArrayList<Integer>();

19: Are primitive data types passed by reference or passed by value?

A.   Passed by value

B.   Passed by reference

20: The pattern described below is known as: public class Foo{ private static Foo instance; private Foo(){ } private static getInstance(){ if (instance == null){ instance = new Foo(); } return instance; }

A.   the Observer Pattern

B.   the Flyweight pattern

C.   the Singleton pattern

D.   the Factory pattern

21: Which of the following JNDI properties provide security information?




D.   All of these

22: How do you declare a destructor in Java?

A.   myClass::~myClass();

B.   You don't need a destructor

C.   @Override System.gc(){ }

D.   @destructor myClass(){ }

23: Can an interface have member variables?

A.   No, never

B.   Yes, as long as they are public, static and final

24: Will a program run if the main() is not static?

A.   No

B.   Yes

25: A JavaBean is a special Java class:

A.   which must be serializable so that the state can be stored.

B.   which must have a public, no-argument constructor.

C.   all of these

D.   which must follow standard naming conventions for attributes such as getXxxx.

26: Do we need to import the java.lang package at anytime?

A.   Yes

B.   No

27: Which of the following is correct way of importing an entire package ‘pkg’?

A.   import pkg

B.   Import pkg.*

C.   import pkg.*

D.   Import pkg

28: which of the following is a correct statement:

A.   a class can implement multiple interface

B.   a class can inherit multiple superclass

C.   a class can inherit multiple abstract superclass

29: What is the syntax for creating a class derived from the class named MyClass?

A.   class MyDerived implements MyClass

B.   public class MyDerived : MyClass

C.   class MyDerived extends MyClass

30: What is the proper syntax for a Java class's main method?

A.   public void static main(String[] args)

B.   private void main (String[] args)

C.   public static main(String[] args)

D.   private static void main(String[] args)

E.   public static void main(String[] args)

31: Which of these keywords is used to define packages in Java?

A.   Package

B.   package

C.   pkg

D.   pack

E.   Pkg

32: Do you need to explicitly define a constructor for every class?

A.   Yes

B.   No

A.   All of these

B.   Within a session, the appropriate sender or publisher or receiver or subscriber objects.

C.   A connection object provided by the JMS server (the message broker)

D.   Within a connection, one or more sessions, which provide a contest for message sending and receiving

34: When you run a Java program, the system is running the Java Runtime Engine (JRE) as an executable which then:

A.   processes the java code through the Java Virtual Machine (JVM).

B.   processes the java code through the native OS interpreter.

C.   process the java code through the interpreter which is pointed to by the CLASSPATH statement.

D.   none of these

35: _____ jumps out of an entire loop, whereas _____ jumps to the next iteration.

A.   stop; jump

B.   stop; continue

C.   break; jump

D.   break; continue

36: Consider the following code snippet String river = new String(“Columbia”); System.out.println(river.length()); What is printed?

A.   river

B.   6

C.   8

D.   Columbia

E.   7

37: The correct operator for 'conditional or' is:

A.   |

B.   &&

C.   &

D.   ||

38: Consider public class MyClass{ public MyClass(){/*code*/} // more code... } To instantiate MyClass, you would write?

A.   MyClass mc = MyClass;

B.   MyClass mc = new MyClass;

C.   MyClass mc = new MyClass();

D.   It can't be done. The constructor of MyClass should be defined as public void MyClass(){/*code*/}

E.   MyClass mc = MyClass();

39: Which keywords would you use to handle exceptions in Java?

A.   throw, take, finally

B.   try, catch, end

C.   throw, catch, end

D.   try, catch, finally

E.   throw, catch, do

40: The process of changing a datatype of a value is called:

A.   typecasting

B.   hardcasting

C.   datatyping

41: The varargs method: public void foo(String... strings) may be called with:

A.   foo("bar1");

B.   foo("bar1", "bar2");

C.   All of these.

D.   foo("bar1", "bar2", "bar3");

42: public class Dog extends Animal{ ... } is an example of...

A.   Encapsulation

B.   Abstraction

C.   Inheritance

D.   Type casting

43: Which of the following is the correct signature for a main method that can be used as an entry point by the Java runtime?

A.   public static void main(String[] args)

B.   public void main(String[] args)

C.   public static void main(Collection c)

D.   public static int main(String[] args)

44: All Java files must have a .java extension and are compiled with the:

A.   gcc compiler

B.   system runtime compiler

C.   javac compiler

45: Class definitions in Java can have which of the following access levels?

A.   protected

B.   public

C.   all of these

D.   private

46: An applet can do which of the following?

A.   All of these

B.   Stop running

C.   Start running

D.   Self initialize

47: You can use the 'static' keyword for which of the following?

A.   Variables

B.   Methods

C.   All of these

D.   Classes

48: As a general syntax rule, Java is case sensitive on:

A.   only Microsoft Windows platforms.

B.   all platforms.

C.   only on UNIX based platforms.

49: What keyword is used to add external package members to the current Java file?

A.   get

B.   import

C.   uses

D.   using

E.   add

50: Which of the following is correct for the "main" method of a class?

A.   public final void main(String arg0, String arg1="default") {}

B.   public static void main(String [] args) { }

C.   public void main(int argc, String [] argv) {}

D.   @Main public void main(int argc, String[] argv) {}