Top 60 Java MCQ questions.
- The JVM stands for:
- Java virtual model
- Java visual model
- Java virtual machine
- Java visual machine
Ans: Java virtual machine
- What is the name of the Java compiler?
- java
- jar
- javac
- javah
Ans: javac
- Java was developed by:
- Netscape Navigator
- Sun Microsystems
- Microsoft
- Linux
Ans: Sun Microsystems
- In Java, small programs that run on browsers are called
- Applications
- Midlets
- Applets
- Servlets
Ans: Applets
- In Java, you can write stand-alone programs known as
- Application
- Midlets
- Applets
- Servlets
Ans:Application
- Which of the following is a valid header for the main() method?
- public static void main()
- public static void main( String args )
- public static void main( String args[] )
- public static int main( String args[] )
Ans: public static void main( String args[] )
- Which of the following is not a java IDE?
- Eclipse
- NetBeans
- JDK
- JBuilder
Ans: JDK
- 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!");
}
}
- javac MyClass
- javac MyClass.java
- javac MyClass.main
- javac MyClass.main()
Ans: javac MyClass.java
- Which command should be used to execute the main method of class MyClass?
- java MyClass
- java MyClass.java
- java MyClass.class
- java MyClass.main()
Ans: java MyClass
- Which java tool creates HTML help files from source code?
- javah
- javap
- javadoc
- java
Ans: javadoc
- Which of the following is an invalid variable name in java?
- case
- value
- _myVar
- $amount
Ans: case
- Consider the declaration: short a. What range of values can be stored in a?
- 0 to 2¹⁶-1
- -2¹⁶ to 2¹⁶-1
- -2¹⁵ to 2¹⁵-1
- 0 to 2¹⁵-1
Ans: -2¹⁵ to 2¹⁵-1
- Consider the declaration: byte b. What range of values can be applied to b?
- 0 to 2⁸-1
- -2⁸ to 2⁸-1
- -2⁷ to 2⁷-1
- 0 to 2⁷-1
Ans: -2⁷ to 2⁷-1
- Which of the following statements will produce an error?
- char ch = ‘B’;
- char ch = ‘ABC’;
- char ch = ‘\u463’;
- char ch = ‘\n’;
Ans: char ch = ‘ABC’;
- What will be the default value of the boolean type variable?
- Null
- True
- False
- 0
Ans: False
- Which of the following is a legal statement?
- int a=10, x=!a;
- int a=10, x=x~a;
- float b=10.5>>2;
- int x=10^20;
Ans: int x = 10^20;
- Consider the declaration: int[] intArray = new int[3]; Which expression gives the number of elements in intArray?
- intArray.size
- intArray.length()
- intArray.length
- intArray.size()
Ans: intArray.length
- 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]);
- 1230
- 123
- 000
- throws ArrayIndexOutOfBoundsException
Ans: throws ArrayIndexOutOfBoundsException
- Which of the following statement is illegal?
- int arr[][] = new int[5][5];
- int[] arr[] = new int[5][5];
- int[][] arr = new int[5][5];
- int[] arr = new int[5][];
Ans: int[] arr = new int[5][];
- 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");
- Nothing is printed.
- Equal A
- Equal B
- Both Equal A and Equal B are printed.
Ans: Equal B
- Which of the following is NOT a method modifier?
- final
- static
- package
- strictfp
Ans: package
- Which is the correct syntax to declare variable-length parameters?
- variable type parameter
- type…parameter
- type variable parameter
- type->parameter
Ans: type..parameter
- 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);
- Compiler error
- 9
- 10
- 19
Ans: 19
- Which of the following methods is similar to a destructor in C++?
- static
- finalize
- public
- final
Ans: finalize
- To access a class within a package, you must use the keyword:
- package
- class
- import
- Access
Ans: import
- Which of the following is not a method of the Object class?
- toString
- clone
- finally
- finalize
Ans: finally
- Which of the following statements is incorrect?
- The native keyword indicates that the method is implemented in another language like C/C++.
- The only statements that can appear before a package statement in a Java file are comments.
- A constructor of a class is defined as void.
- A class constructor may have a public or protected keyword before them, nothing else.
Ans: A constructor of a class is defined as void.
- Which of the following statements are incorrect?
- An abstract class may not be instantiated.
- The final keyword indicates that the code is written in the non-Java language, typically C/C++.
- A static variable indicates there is only one copy of that variable.
- 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.
- How many objects of MyClass are created by the following code?
MyClass [] arr = new MyClass [5];
MyClass ml = new MyClass ();
MyClass m2 = m1;
- 7
- 6
- 2
- 1
Ans: 1
- Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
- final
- abstract
- native
- static
Ans: static
- Which method of the Integer class can be used to convert an Integer object to a primitive int type?
- getInt
- valueOf
- intValue
- getInteger
Ans: intValue
- Which wrapper class constant gives the smallest value of that type?
- MINIMUM_VALUE
- SMALL_VALUE
- MIN_VALUE
- TINY_VALUE
Ans: MIN_VALUE
- Which of the following statements are true?
- A static method may be invoked before any object is created.
- A static method cannot access non-static methods of the class.
- Final modifiers can appear before a class or a variable but not before a method.
- 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.
- 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 ");
- Tree
- Tree Oak
- Oak
- No output
Ans: Tree Oak
- Which of the following statements is false?
- An abstract class cannot be instantiated
- An abstract class cannot be extended
- A final class cannot be subclassed
- A final method cannot be overridden
Ans: An abstract class cannot be extended
- Which of the following is a valid class definition?
- class Animal {abstract void growl(); }
- abstract Animal {abstract void growl();}
- abstract class Animal {abstract void growl();}
- abstract class Animal {abstract void growl() {System.out.println(“growl”);}}
Ans: abstract class Animal {abstract void growl();}
- Select the correct answer for the following code.
class A{}
class B extends A{ }
A a = new A() ;
B b = new B() ;
b = a;
- Illegal at compile-time, legal at runtime
- Legal at compile-time, Illegal at runtime
- Legal at compile-time, Legal at runtime
- Illegal at compile-time, illegal at runtime
Ans: Illegal at compile-time, illegal at runtime
- Which of the following statements are true?
- A subclass must define all the methods from the superclass.
- A subclass can define a method with the different names and parameters as a superclass method.
- A subclass can define a field with the same name as a field defined by the superclass.
- 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.
- Which of the following statements are true?
- Private methods cannot be overridden in subclasses.
- A subclass can override any method in a superclass.
- The parameter list of an overriding method must be a subset of the parameter list of the
- 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.
- 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?
- A.doit();
- super.super.doit();
- super.doit() ;
- it cannot be invoked.
Ans: it cannot be invoked.
- 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.
- void m() throws IOException{}
- void m() throw IOException{}
- void m(void) throws IOException{}
- m() throws IOException { }
- void m() {} throws IOException
Ans:
a. void m() throws IOException{}
c. void m(void) throws IOException{}
- 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])}
- myprog
- good
- morning
- Exception: “java.lang.ArrayIndexOutOfBoundsException: 2”
Ans: morning
- 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");
}
- Compiler error complaining about the catch block where no Throwable object can ever be thrown.
- Compiler error – Throwable not found. It must be imported in the first line of the code.
- Inside catch.
- 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.
- The java.lang.Exception class?
- Extends Throwable
- Implements Throwable
- Extends Error;
- Extends RuntimeException
Ans: Extends Throwable
- Given: int x = Integer.parseInt(str);
Which exception should be handled for this statement?
- IOException
- NumberFormatException
- ArrayIndexOutOfBoundsException
- ClassCastException
Ans: NumberFormatException
- 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?
- The code must be compiled with the -source 1.4 option if you are using JDK 5.0.
- The program must be executed with the -ea option.
- At least one argument must be given in the execution command.
- 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.
- Which of the following is true about assertions in Java?
- You use assertions to report errors to the users of an application.
- Assertions are mostly used during testing to uncover internal program errors.
- An assertion error is thrown if the condition specified by <condition> in assert is true.
- 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.
- 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?
- i was in try i was in finally
- i was in try
- i was in finally
- compiler error: try without catch.
Ans: i was in try i was in finally
- While navigating the file system by using the methods of the File class, which of the following operations can you perform?
- Delete a file
- Change directory
- Create a file
- Check if it is hidden
Ans: Change directory
- How will you write an object to a file?
- Using ObjectInputStream
- Using ObjectOutputStream
- Using ObjectInputStream and FileInputStream
- Using ObjectOutputStream and FileOutputStream
Ans: Using ObjectOutputStream and FileOutputStream
- Which of the following streams will enable you to read primitive data from a file?
- DataInputStream and FileInputStream
- FileReader
- FileInputStream
- DataInputStream
Ans: DataInputStream and FileInputStream
- Which abstract class is the superclass of all classes used for reading bytes?
- InputStream
- OutputStream
- Reader
- Writer
Ans: InputStream
- Which of these is NOT an abstract class?
- InputStream
- Reader
- FileReader
- FilterWriter
Ans: Reader
- A directory can be created using a method of which class?
- File
- FileReader
- FileInputStream
- File Writer
Ans: File
- Which of the following is a valid file opening mode?
- w
- rw
- r+
- w+
Ans: rw
- Which method is used to position the file pointer in RandomAccessFile?
- fseek()
- seek()
- position()
- move()
Ans: seek()
- Which of the following statements assigns “Hello Java” to the String variable s?
- String s = “Hello Java”;
- String s[] = “Hello Java”;
- new String s = “Hello Java”;
- String s = new String(“Hello Java”);
Ans:
a. String s = “Hello Java”;
d. String s = new String(“Hello Java”);
- Which of the following can contain a menu bar?
- JPanel
- JFrame
- JTextArea
- JLabel
Ans: JFrame