Home » Interview Questions and Answers » Java MCQ questions

Java MCQ questions

Top 60 Java MCQ questions.

  1. The JVM stands for:
    1. Java virtual model
    2. Java visual model
    3. Java virtual machine
    4. Java visual machine

Ans: Java virtual machine

  1. What is the name of the Java compiler?
    1. java
    2. jar
    3. javac
    4. javah

Ans: javac

  1. Java was developed by:
    1. Netscape Navigator 
    2. Sun Microsystems
    3. Microsoft
    4. Linux

Ans: Sun Microsystems

  1. In Java, small programs that run on browsers are called
    1. Applications
    2. Midlets
    3. Applets
    4. Servlets

Ans: Applets

  1. In Java, you can write stand-alone programs known as
    1. Application
    2. Midlets
    3. Applets
    4. Servlets

Ans:Application

  1. Which of the following is a valid header for the main() method?
    1. public static void main() 
    2. public static void main( String args )
    3. public static void main( String args[] )
    4. public static int main( String args[] )

Ans: public static void main( String args[] )

  1. Which of the following is not a java IDE?
    1. Eclipse
    2. NetBeans
    3. JDK
    4. JBuilder

Ans: JDK

  1. Which command in the Java 2 SDK should be used to compile the following code in file “MyClass.java”
public class MyClass
{
public static void main (String [] args) 
{
 System.out.println ("Welcome to Java!");
}
}
  1. javac MyClass
  2. javac MyClass.java
  3. javac MyClass.main
  4. javac MyClass.main()

Ans: javac MyClass.java

  1. Which command should be used to execute the main method of class MyClass?
    1. java MyClass
    2. java MyClass.java
    3. java MyClass.class
    4. java MyClass.main()

Ans: java MyClass

  1. Which java tool creates HTML help files from source code?
    1. javah
    2. javap
    3. javadoc
    4. java

Ans: javadoc

  1. Which of the following is an invalid variable name in java?
    1. case
    2. value
    3. _myVar
    4. $amount

Ans: case

  1. Consider the declaration: short a. What range of values can be stored in a?
    1. 0 to 2¹⁶-1
    2. -2¹⁶ to 2¹⁶-1
    3. -2¹⁵ to 2¹⁵-1
    4. 0 to 2¹⁵-1

Ans: -2¹⁵ to 2¹⁵-1

  1. Consider the declaration: byte b. What range of values can be applied to b?
    1. 0 to 2⁸-1
    2. -2⁸ to 2⁸-1
    3. -2⁷ to 2⁷-1
    4. 0 to 2⁷-1

Ans: -2⁷ to 2⁷-1

  1. Which of the following statements will produce an error?
    1. char ch = ‘B’;
    2. char ch = ‘ABC’;
    3. char ch = ‘\u463’;
    4. char ch = ‘\n’;

Ans: char ch = ‘ABC’;

  1. What will be the default value of the boolean type variable?
    1. Null
    2. True
    3. False
    4. 0

Ans: False

  1. Which of the following is a legal statement?
    1. int a=10, x=!a;
    2. int a=10, x=x~a;
    3. float b=10.5>>2;
    4. int x=10^20;

Ans: int x = 10^20;

  1. Consider the declaration: int[] intArray = new int[3]; Which expression gives the number of elements in intArray?
    1. intArray.size
    2. intArray.length()
    3. intArray.length
    4. intArray.size()

Ans: intArray.length

  1. What will be the output of the following piece of code? 
int[] intArray {1, 2, 3} ;
for (int i=0; i<=intArray.length; i++) 
System.out.print(" "+intArray[i]);
  1. 1230
  2. 123
  3. 000
  4. throws ArrayIndexOutOfBoundsException

Ans: throws ArrayIndexOutOfBoundsException

  1. Which of the following statement is illegal?
    1. int arr[][] = new int[5][5]; 
    2. int[] arr[] = new int[5][5];
    3. int[][] arr = new int[5][5];
    4. int[] arr = new int[5][];

Ans: int[] arr = new int[5][];

  1. What will be the output of the following code?
String s = new String ( "Computer") ; 
if(s == "Computer")
System.out.println ("Equal A");
if( s.equals ("Computer"))
System.out.println("Equal B");
  1. Nothing is printed.
  2. Equal A
  3. Equal B
  4. Both Equal A and Equal B are printed.

Ans: Equal B

  1. Which of the following is NOT a method modifier?
    1. final
    2. static
    3. package
    4. strictfp

Ans: package

  1. Which is the correct syntax to declare variable-length parameters?
    1. variable type parameter
    2. type…parameter
    3. type variable parameter
    4. type->parameter

Ans: type..parameter

  1. What will happen if you attempt to compile and run the following code?
Integer ten=new Integer (10); 
Long nine=new Long (9);
System.out.println (ten + nine);
  1. Compiler error
  2. 9
  3. 10
  4. 19

Ans: 19

  1. Which of the following methods is similar to a destructor in C++?
    1. static
    2. finalize
    3. public 
    4. final

Ans: finalize

  1. To access a class within a package, you must use the keyword:
    1. package
    2. class
    3. import
    4. Access

Ans: import

  1. Which of the following is not a method of the Object class?
    1. toString
    2. clone
    3. finally
    4. finalize

Ans: finally

  1. Which of the following statements is incorrect?
    1. The native keyword indicates that the method is implemented in another language like C/C++.
    2. The only statements that can appear before a package statement in a Java file are comments.
    3. A constructor of a class is defined as void.
    4. A class constructor may have a public or protected keyword before them, nothing else.

Ans: A constructor of a class is defined as void.

  1. Which of the following statements are incorrect?
    1. An abstract class may not be instantiated.
    2. The final keyword indicates that the code is written in the non-Java language, typically C/C++.
    3. A static variable indicates there is only one copy of that variable.
    4. A member defined as private indicates that it is accessible only to other classes in the same package.

Ans: 

b. The final keyword indicates that the code is written in the non-Java language, typically C/C++.

d. A member defined as private indicates that it is accessible only to other classes in the same package.

  1. How many objects of MyClass are created by the following code? 
MyClass [] arr = new MyClass [5]; 
MyClass ml = new MyClass (); 
MyClass m2 = m1;
  1. 7
  2. 6
  3. 2
  4. 1

Ans: 1

  1. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
    1. final
    2. abstract
    3. native
    4. static

Ans: static

  1. Which method of the Integer class can be used to convert an Integer object to a primitive int type?
    1. getInt
    2. valueOf
    3. intValue
    4. getInteger

Ans: intValue

  1. Which wrapper class constant gives the smallest value of that type?
    1. MINIMUM_VALUE
    2. SMALL_VALUE
    3. MIN_VALUE
    4. TINY_VALUE

Ans: MIN_VALUE

  1. Which of the following statements are true?
    1. A static method may be invoked before any object is created.
    2. A static method cannot access non-static methods of the class.
    3. Final modifiers can appear before a class or a variable but not before a method.
    4. The abstract modifier can appear before a class or a method but not before a variable.

Ans: Final modifiers can appear before a class or a variable but not before a method.

  1. What is the output of the following code outline?
class Tree { }
class Oak extends Tree { }
Tree t = new Oak () ;
if (t instanceof Tree)
System.out.print (" Tree ");
if(t instanceof Oak)
System.out.print(" Oak ");
  1. Tree
  2. Tree Oak
  3. Oak
  4. No output

Ans: Tree Oak

  1. Which of the following statements is false?
    1. An abstract class cannot be instantiated
    2. An abstract class cannot be extended 
    3. A final class cannot be subclassed
    4. A final method cannot be overridden 

Ans: An abstract class cannot be extended 

  1. Which of the following is a valid class definition?
    1. class Animal {abstract void growl(); }
    2. abstract Animal {abstract void growl();}
    3. abstract class Animal {abstract void growl();}
    4. abstract class Animal {abstract void growl() {System.out.println(“growl”);}}

Ans: abstract class Animal {abstract void growl();}

  1. Select the correct answer for the following code.
class A{} 
class B extends A{ }
A a = new A() ;
B b = new B() ; 
b = a;
  1. Illegal at compile-time, legal at runtime
  2. Legal at compile-time, Illegal at runtime
  3. Legal at compile-time, Legal at runtime
  4. Illegal at compile-time, illegal at runtime

Ans: Illegal at compile-time, illegal at runtime

  1. Which of the following statements are true?
    1. A subclass must define all the methods from the superclass.
    2. A subclass can define a method with the different names and parameters as a superclass method.
    3. A subclass can define a field with the same name as a field defined by the superclass.
    4. Two classes can be the superclass of each other.

Ans: A subclass can define a field with the same name as a field defined by the superclass.

  1. Which of the following statements are true? 
    1. Private methods cannot be overridden in subclasses.
    2. A subclass can override any method in a superclass.
    3. The parameter list of an overriding method must be a subset of the parameter list of the
    4. A method that it is overriding. The overriding method can have a different return type than the overridden method.

Ans: Private methods cannot be overridden in subclasses.

  1. Given classes A, B, and C, where B extends A, and C extends B, and where all classes implement the instance method void doIt(). How can the doIt() method in A be called from an instance method in C?
    1. A.doit();
    2. super.super.doit();
    3. super.doit() ;
    4. it cannot be invoked.

Ans: it cannot be invoked.

  1. Which of these is a legal definition of a method named m, assuming it throws IOException and returns void. Also, assume that the method does not take any arguments. Select the one correct answer.
    1. void m() throws IOException{}
    2. void m() throw IOException{}
    3. void m(void) throws IOException{}
    4. m() throws IOException { }
    5. void m() {} throws IOException

Ans: 

a. void m() throws IOException{}

c. void m(void) throws IOException{}

  1. What will be printed out if the main in class myprog is run with the following command line: java myprog good morning
public static void main (String args [] ) 
{System.out.println (argv[2])}
  1. myprog
  2. good
  3. morning
  4. Exception: “java.lang.ArrayIndexOutOfBoundsException: 2”

Ans: morning

  1. What will happen when the following code is executed? 
public static void main (String args []) {
        System.out.println ("Before Try");
try
{
}
catch (Throwable t) 
{
        System.out.println ("Inside Catch");
}
        System.out.println ("At the End");
}
  1. Compiler error complaining about the catch block where no Throwable object can ever be thrown.
  2. Compiler error – Throwable not found. It must be imported in the first line of the code.
  3. Inside catch.
  4. No compiler error. The lines “Before Try” and “At the end” are printed on the screen.

Ans: No compiler error. The lines “Before Try” and “At the end” are printed on the screen.

  1. The java.lang.Exception class?
    1. Extends Throwable
    2. Implements Throwable
    3. Extends Error;
    4. Extends RuntimeException

Ans:  Extends Throwable

  1. Given: int x = Integer.parseInt(str); 

Which exception should be handled for this statement?

  1. IOException
  2. NumberFormatException 
  3. ArrayIndexOutOfBoundsException
  4. ClassCastException

Ans: NumberFormatException

  1. Consider the following code fragment: 
public class AssertionExample2
{
public static void main (String[]args) { 
System.out.println (args.length); 
assert args.length != 0;
}
}

Which of the following must be done for the code to throw an AssertionError?

  1. The code must be compiled with the -source 1.4 option if you are using JDK 5.0.
  2. The program must be executed with the -ea option. 
  3. At least one argument must be given in the execution command.
  4. No argument should be given in the execution command.

Ans: 

b. The program must be executed with the -ea option. 

d. No argument should be given in the execution command.

  1. Which of the following is true about assertions in Java? 
    1. You use assertions to report errors to the users of an application.
    2. Assertions are mostly used during testing to uncover internal program errors. 
    3. An assertion error is thrown if the condition specified by <condition> in assert is true. 
    4. An assertion error is thrown if the condition specified by <condition> in assert is false.

Ans: 

b. Assertions are mostly used during testing to uncover internal program errors.

d. An assertion error is thrown if the condition specified by <condition> in assert is false.

  1. Consider the following code fragment.
public class TryFinallyTest{ 
public static void main (String[] args) { 
try
{
System.out.print ("I was in try
}
 finally 
{
System.out.print ("I was in finally");
}
}
}

What is the result of executing this code?

  1. i was in try i was in finally
  2. i was in try
  3. i was in finally
  4. compiler error: try without catch.

Ans: i was in try i was in finally

  1. While navigating the file system by using the methods of the File class, which of the following operations can you perform?
    1. Delete a file
    2. Change directory
    3. Create a file
    4. Check if it is hidden

Ans: Change directory

  1. How will you write an object to a file? 
    1. Using ObjectInputStream
    2. Using ObjectOutputStream
    3. Using ObjectInputStream and FileInputStream
    4. Using ObjectOutputStream and FileOutputStream

Ans: Using ObjectOutputStream and FileOutputStream

  1. Which of the following streams will enable you to read primitive data from a file?
    1. DataInputStream and FileInputStream 
    2. FileReader
    3. FileInputStream 
    4. DataInputStream

Ans: DataInputStream and FileInputStream

  1. Which abstract class is the superclass of all classes used for reading bytes?
    1. InputStream
    2. OutputStream 
    3. Reader
    4. Writer

Ans: InputStream

  1. Which of these is NOT an abstract class?
    1. InputStream
    2. Reader
    3. FileReader
    4. FilterWriter

Ans: Reader

  1. A directory can be created using a method of which class?
    1. File
    2. FileReader
    3. FileInputStream
    4. File Writer

Ans: File

  1. Which of the following is a valid file opening mode?
    1. w
    2. rw
    3. r+
    4. w+

Ans: rw

  1. Which method is used to position the file pointer in RandomAccessFile?
    1. fseek()
    2. seek()
    3. position()
    4. move()

Ans: seek()

  1. Which of the following statements assigns “Hello Java” to the String variable s?
    1. String s = “Hello Java”;
    2. String s[] = “Hello Java”;
    3. new String s = “Hello Java”;
    4. String s = new String(“Hello Java”);

Ans: 

a. String s = “Hello Java”;

d. String s = new String(“Hello Java”);

  1. Which of the following can contain a menu bar?
    1. JPanel
    2. JFrame
    3. JTextArea
    4. JLabel

Ans: JFrame

Pin It on Pinterest