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 = a.new 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?

A.   java.naming.security.credential

B.   java.naming.security.authentication

C.   java.naming.security.principal

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) {}

51: What company developed java?

A.   Google

B.   Microsoft

C.   IBM

D.   Facebook

E.   Sun Microsystems (Oracle)

52: What effect does declaring a method as final have?

A.   The parameters passed to the method cannot be modified.

B.   The method cannot be overridden in subclasses.

C.   The return value of the method will be final.

D.   The method can only accept final parameters.

53: If I write return at the end of the try block, will the finally block still execute?

A.   Yes

B.   No

54: Under the strictest definition, an interface may contain only:

A.   additional pylons

B.   abstract methods

C.   fields

D.   constructors

55: How do you prevent a class from being extended by a subclass?

A.   Declare the class as abstract.

B.   Declare the class as final.

C.   Declare all member variables and methods as protected.

D.   Declare all its members as private.

56: Can you declare an abstract class with no abstract methods?

A.   Yes

B.   No

57: ________ is when a subclass implements a method that is already provided by a superclass.

A.   Method overloading

B.   Object orientation overdev

C.   Method overdriving

D.   Method overriding

58: What is a token?

A.   A token is any character that may be used as punctuation

B.   A token is a type of expression

C.   A token is a group of characters that means something in a particular context.

D.   A token is a group of digits

59: What is displayed when the following code is compiled and executed? String s1 = new String("Test"); String s2 = new String("Test"); if (s1==s2) System.out.println("Same"); if (s1.equals(s2)) System.out.println("Equals");

A.   Equals

B.   Same Equals

C.   The code compiles, but nothing is displayed upon execution.

D.   Same

60: A method is considered to be overloaded when:

A.   they have multiple call signatures

B.   the method throws an exception

C.   they have different return types

D.   You cannot overload a method

61: Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?

A.   x.add(1, "Chicago")

B.   x.add("Chicago")

C.   x.add(2, "Chicago")

D.   x.add(0, "Chicago")

62: Can we override a static method?

A.   Yes

B.   No

63: Java allows you to specify two types of variables: primitive, which store a single value, and

A.   neither of these

B.   reference, where the data is accessed indirectly.

C.   wide, which reference a double byte variable.

64: output of program class Test {T obj; Test(T obj) { this.obj=obj } T getobj() {return obj; } } class RunC {public static void main(String[] ar) { Test obj=new Test("hello"); System.out.print(obj.getobj()); } }

A.   Runtime Error

B.   hello

C.   Nothing display

D.   error:class decleration not valid

65: Can a class be both abstract and final?

A.   Yes

B.   No

66: Which Map implementation is safe for modification in a multi-threaded program?

A.   java.util.WeakHashMap

B.   java.util.concurrent.ConcurrentHashMap

C.   java.util.TreeMap

D.   java.util.LinkedHashMap

67: TreeMap class is used to implement which collection interface?

A.   List

B.   SortedSet

C.   Set

D.   SortedMap

68: Which one of these primitive types is unsigned?

A.   double

B.   char

C.   long

D.   float

E.   int

69: Can you mark an interface as final?

A.   No

B.   Yes

70: What is the difference between Vector and ArrayList?

A.   Vector is synchronized whereas ArrayList is not

B.   ArrayList is synchronized whereas Vector is not

C.   Both are identical

71: Any valid program compiled under one version of Java...

A.   ...will generally run under previous versions of the platform.

B.   ...will generally run under future versions of the platform.

C.   ...can never run under future versions of the platform.

D.   ...can only run under that version of the platform.

72: Which of the following is *not* a method in java.lang.String?

A.   String toString()

B.   int compareTo(String anotherString)

C.   boolean isNull()

D.   String valueOf(char[] data)

73: What is the command to run an executable JAR file named program.jar?

A.   java -jar program.jar

B.   java -run program.jar

C.   jar program.jar

D.   java -run program

E.   java -jar program

74: The "synchronized" keyword does the following

A.   Provides a convenient shortcut for creating threads

B.   Prohibits code from being executed during garbage collection

C.   Aligns samples within the JVM for accuracy when profiling

D.   Ensures that each call finishes at the same time

E.   Prevents concurrency within methods or statements

75: If "A" is a class, then what does the statement "A a1;" do?

A.   The syntax is incorrect.

B.   The statement does not do anything.

C.   The object reference variable 'a1' should be in title case.

D.   An object 'a1' of class 'A' is created.

E.   The object reference variable 'a1' is declared.

76: Can a non-generic class have a generic constructor?

A.   No

B.   Yes

77: Java's Reflection feature allows you to do things including (but not limited to) :

A.   Perform pseudo-pointer manipulations in Java.

B.   Dynamically change the Java Heap Size.

C.   Optimize recursion at runtime by extending the reflection class.

D.   Obtain the names of a class' members and display them at runtime.

78: What is transient variable?

A.   It is the default variable in the class.

B.   A variable which is not static.

C.   A variable which is serialized.

D.   A variable which is not serialized.

79: The foreach loop in Java is constructed as:

A.   for (Object o : collection)

B.   foreach (Object o : collection)

C.   there is no foreach loop in Java

D.   for (Object o in collection)

E.   foreach (collection as Object o)

80: Java programs must have at least one method called main() _____.

A.   In order to compile

B.   Both of these

C.   In order to execute

81: If you set one object variable equal to another object variable:

A.   you end up with two copies of the data and two references to the variable.

B.   you end up with one copy of the data and one reference to the data.

C.   you end up with one copy of the data and two references to the data.

82: If you override method "equals()", what other function you must override for the class to work properly?

A.   public int hash()

B.   public static int hash()

C.   public int hashCode()

D.   public static int hashCode()

83: String objects in Java are immutable which means:

A.   that, if constant, they cannot be changed once they are created.

B.   that they can be changed one time without changing the reference.

C.   that they can be changed multiple times without altering the reference.

84: Which of these keywords is used to manually clean up memory in java?

A.   finalizeGarbageCollection

B.   final

C.   finalize

D.   finally

85: Consider the following code: int i=aReader.read(); What is the true of the type of variable aReader?

A.   It has to be a FileReader

B.   It has to be a BufferReader.

C.   It can neither be a FileReader or a BufferReader.

D.   It can either be a FileReader or a BufferReader.

86: Any source code valid for one version of Java is also valid for all previous versions of Java.

A.   True

B.   False

87: What is an abstract class?

A.   A class with an undefined constructor

B.   A class that does not define all of its methods

C.   A class of methods that must be implemented

D.   A class that is defined by another class

88: In Math and other java.lang Classes, generally all methods are declared _.

A.   static

B.   abstract

C.   private

D.   final

E.   package private

89: Which method is used to sort a Collection by natural order of its elements?

A.   Sort.sort

B.   Collection.sort

C.   CollectionsUtils.sortCollection

D.   Collections.sort

90: What is the keyword to forbid the serialization of an instance field?

A.   transient

B.   volatile

C.   serializable

D.   break

E.   synchronized

91: Java interfaces can extend...

A.   final classes.

B.   one other interface.

C.   nothing: extension is not valid for interfaces.

D.   multiple interfaces.

92: SWING Toolkit contains

A.   component set (subclasses of JComponent),support classes,interfaces

B.   interfaces,component set (subclasses of JComponent)

C.   component set (subclasses of JComponent)

D.   support classes,interfaces

93: Is "main" a keyword in Java?

A.   No

B.   Yes

94: Which of these is the wildcard symbol for use in generic type specification?

A.   &

B.   !

C.   ?

D.   %

95: what are the parts of JNDI architecture

A.   JNDI SPI

B.   Java API

C.   JNDI API and JNDI SPI

D.   JNDI API

96: public class SomeClass { public static void main(String[] args) { System.out.println((String)null); } } What is the result of the following program?

A.   The code prints "null" on console.

B.   The code throws NullPointerException.

C.   The code does not compile.

97: Non-static attributes can be accessed without an instance of the class.

A.   True

B.   Only if they are private

C.   False

D.   Only if they are public

98: public class SomeClass { public static void main(String[] args) { System.out.println(((String)null).toUpperCase()); } } What is the result of the following program?

A.   The code throws NullPointerException.

B.   The code prints NULL on console.

C.   The code does not compile.

99: Are static fields of a class included in serialization?

A.   No

B.   Yes

100: What does a synchronized method use as a mutex in Java?

A.   A globally declared mutex.

B.   The method's mutex.

C.   The owning object's (this's) mutex.