250+ TOP MCQs on JDK-JRE-JIT-JVM and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “JDK-JRE-JIT-JVM”.

1. Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: b
Clarification: JDK is a core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program.

2. Which component is responsible for converting bytecode into machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: a
Clarification: JVM is responsible to converting bytecode to the machine specific code. JVM is also platform dependent and provides core java functions like garbage collection, memory management, security etc.

3. Which component is responsible to run java program?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: d
Clarification: JRE is the implementation of JVM, it provides platform to execute java programs.

4. Which component is responsible to optimize bytecode to machine code?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: c
Clarification: JIT optimizes bytecode to machine specific language code by compiling similar bytecodes at the same time. This reduces overall time taken for compilation of bytecode to machine specific language.

5. Which statement is true about java?
a) Platform independent programming language
b) Platform dependent programming language
c) Code dependent programming language
d) Sequence dependent programming language

Answer: a
Clarification: Java is called ‘Platform Independent Language’ as it primarily works on the principle of ‘compile once, run everywhere’.

6. Which of the below is invalid identifier with the main method?
a) public
b) static
c) private
d) final

Answer: c
Clarification: main method cannot be private as it is invoked by external method. Other identifier are valid with main method.

7. What is the extension of java code files?
a) .class
b) .java
c) .txt
d) .js

Answer: b
Clarification: Java files have .java extension.

8. What is the extension of compiled java classes?
a) .class
b) .java
c) .txt
d) .js

Answer: a
Clarification: The compiled java files have .class extension.

9. How can we identify whether a compilation unit is class or interface from a .class file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type

Answer: a
Clarification: The Java source file contains a header that declares the type of class or interface, its visibility with respect to other classes, its name and any superclass it may extend, or interface it implements.

10. What is use of interpreter?
a) They convert bytecode to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
d) It is a synonym for JIT

Answer: b
Clarification: Interpreters read high level language (interprets it) and execute the program. Interpreters are normally not passing through byte-code and jit compilation.

250+ TOP MCQs on The Object Class and Answers

Java MCQs on Object class of Java Programming Language.

1. Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class

Answer: b
Clarification: Object class is superclass of every class in Java.

2. Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()

Answer: c
Clarification: None.

3. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned

Answer: c
Clarification: None.

4. Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final

Answer: d
Clarification: Declaring a class final implicitly declared all of its methods final, and makes the class inheritable.

5. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned

Answer: a
Clarification: A abstract class is incomplete by itself and relies upon its subclasses to provide a complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class.

6. Which of these class relies upon its subclasses for complete implementation of its methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned

Answer: b
Clarification: None.

7. What will be the output of the following Java program?

  1.     abstract class A 
  2.     {
  3.         int i;
  4.         abstract void display();
  5.     }    
  6.     class B extends A 
  7.     {
  8.         int j;
  9.         void display() 
  10.         {
  11.             System.out.println(j);
  12.         }
  13.     }    
  14.     class Abstract_demo 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.j=2;
  20.             obj.display();    
  21.         }
  22.     }

a) 0
b) 2
c) Runtime Error
d) Compilation Error

Answer: b
Clarification: class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:

$ javac Abstract_demo.java
$ java Abstract_demo 
2

8. What will be the output of the following Java program?

  1.    class A 
  2.    {
  3. 	int i;
  4. 	int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16.              A obj2 = new A();
  17. 	     System.out.print(obj1.equals(obj2));
  18.         }
  19.    }

a) false
b) true
c) 1
d) Compilation Error

Answer: a
Clarification: obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.
output:

$ javac Output.java
$ java Output
false

9. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.              Object obj = new Object();
  6. 	     System.out.print(obj.getclass());
  7.         }
  8.     }

a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error

Answer: c
Clarification: None.
output:

$ javac Output.java
$ java Output
class java.lang.Object

10. What will be the output of the following Java code?

  1.    class A 
  2.    {
  3.         int i;
  4. 	int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16. 	     System.out.print(obj1.toString());
  17.         }
  18.    }

a) true
b) false
c) String associated with obj1
d) Compilation Error

Answer: c
Clarification: toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.
output:

250+ TOP MCQs on Java.io Byte Streams and Answers

Java MCQs on java.io byte streams of Java Programming Language.

1. Which of these classes is used for input and output operation when working with bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned

Answer: a
Clarification: InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.

2. Which of these class is used to read and write bytes in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader

Answer: c
Clarification: None.

3. Which of these method of InputStream is used to read integer representation of next available byte input?
a) read()
b) scanf()
c) get()
d) getInteger()

Answer: a
Clarification: None.

4. Which of these data type is returned by every method of OutputStream?
a) int
b) float
c) byte
d) none of the mentioned

Answer: d
Clarification: Every method of OutputStream returns void and throws an IOExeption in case of errors.

5. Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()

Answer: b
Clarification: None.

6. Which of these method(s) is/are used for writing bytes to an outputstream?
a) put()
b) print() and write()
c) printf()
d) write() and read()

Answer: b
Clarification: write() and print() are the two methods of OutputStream that are used for printing the byte data.

7. What will be the output of the following Java program? (Note: inputoutput.java is stored in the disk.)

  1.     import java.io.*;
  2.     class filesinputoutput 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             InputStream obj = new FileInputStream("inputoutput.java");
  7.             System.out.print(obj.available());
  8.         }
  9.     }

a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file

Answer: c
Clarification: obj.available() returns the number of bytes.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
1422

(Output will be different in your case)

8. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if(i == 0) 
  15.                    {
  16.             	       System.out.print((char)c); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

a) abc
b) ABC
c) ab
d) AB

Answer: a
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
abc

9. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c)); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

a) abc
b) ABC
c) ab
d) AB

Answer: b
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
ABC

10. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c));
  17.                        obj2.write(1); 
  18.             	   }
  19.                }
  20.                System.out.print(obj2);
  21.            }
  22.         }
  23.     }

a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa

Answer: d
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

250+ TOP MCQs on Networking – httpd.java Class and Answers

Java MCQs on httpd.java of Java Programming Language.

1. Which of these methods of httpd class is used to read data from the stream?
a) getDta()
b) GetResponse()
c) getStream()
d) getRawRequest()

Answer: d
Clarification: The getRawRequest() method reads data from a stream until it gets two consecutive newline characters.

2. Which of these method of httpd class is used to get report on each hit to HTTP server?
a) log()
b) logEntry()
c) logHttpd()
d) logResponse()

Answer: b
Clarification: None.

3. Which of these methods are used to find a URL from the cache of httpd?
a) findfromCache()
b) findFromCache()
c) serveFromCache()
d) getFromCache()

Answer: c
Clarification: serveFromCatche() is a boolean method that attempts to find a particular URL in the cache. If it is successful then the content of that cache entry are written to the client, otherwise it returns false.

4. Which of these variables stores the number of hits that are successfully served out of cache?
a) hits
b) hitstocache
c) hits_to_cache
d) hits.to.cache

Answer: d
Clarification: None.

5. Which of these method of httpd class is used to write UrlCacheEntry object into local disk?
a) writeDiskCache()
b) writetoDisk()
c) writeCache()
d) writeDiskEntry()

Answer: a
Clarification: The writeDiskCache() method takes an UrlCacheEntry object and writes it persistently into the local disk. It constructs directory names out of URL, making sure to replace the slash(/) characters with system dependent seperatorChar.

6. What will be the output of the following Java program?

  1.     import java.net.*;
  2.     class networking 
  3.     {
  4.         public static void main(String[] args) throws Exception 
  5.         {
  6.             URL obj = new URL("javamcq");
  7.             URLConnection obj1 = obj.openConnection();
  8.             int len = obj1.getContentLength();
  9.             System.out.print(len);
  10.         }
  11.     }

Note: Host URL is having length of content 127.
a) 126
b) 127
c) Compilation Error
d) Runtime Error

Answer: b
Clarification: None.
Output:

$ javac networking.java
$ java networking 
127

7. Which of these method is used to start a server thread?
a) run()
b) start()
c) runThread()
d) startThread()

Answer: a
Clarification: run() method is called when the server thread is started.

8. Which of these method is called when http daemon is acting like a normal web server?
a) Handle()
b) HandleGet()
c) handleGet()
d) Handleget()

Answer: c
Clarification: None.

250+ TOP MCQs on Collection Algorithms and Answers

Java MCQs on collection algorithms of Java Programming Language.

1. Which of these is an incorrect form of using method max() to obtain a maximum element?
a) max(Collection c)
b) max(Collection c, Comparator comp)
c) max(Comparator comp)
d) max(List c)

Answer: c
Clarification: Its illegal to call max() only with comparator, we need to give the collection to be searched into.

2. Which of these methods sets every element of a List to a specified object?
a) set()
b) fill()
c) Complete()
d) add()

Answer: b
Clarification: None.

3. Which of these methods can randomize all elements in a list?
a) rand()
b) randomize()
c) shuffle()
d) ambiguous()

Answer: c
Clarification: shuffle – randomizes all the elements in a list.

4. Which of these methods can convert an object into a List?
a) SetList()
b) ConvertList()
c) singletonList()
d) CopyList()

Answer: c
Clarification: singletonList() returns the object as an immutable List. This is an easy way to convert a single object into a list. This was added by Java 2.0.

5. Which of these is true about unmodifiableCollection() method?
a) unmodifiableCollection() returns a collection that cannot be modified
b) unmodifiableCollection() method is available only for List and Set
c) unmodifiableCollection() is defined in Collection class
d) none of the mentioned

Answer: b
Clarification: unmodifiableCollection() is available for al collections, Set, Map, List etc.

6. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12. 	    while(i.hasNext())
  13. 	        System.out.print(i.next() + " ");
  14.         }
  15.     }

a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5

Answer: a
Clarification: None.
Output:

$ javac Collection_Algos.java
$ java Collection_Algos
2 8 5 1

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    while(i.hasNext())
  14. 	        System.out.print(i.next() + " ");
  15.         }
  16.     }

a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5

Answer: b
Clarification: Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after reversing it became 1->5->8->2.
Output:

$ javac Collection_Algos.java
$ java Collection_Algos
1 5 8 2

contest

250+ TOP MCQs on Writing Console Output and Answers

Java MCQs on writing console outputs in Java Programming Language.

1. Which of these class contains the methods print() & println()?
a) System
b) System.out
c) BUfferedOutputStream
d) PrintStream

Answer: d
Clarification: print() and println() are defined under the class PrintStream, System.out is the byte stream used by these methods .

2. Which of these methods can be used to writing console output?
a) print()
b) println()
c) write()
d) all of the mentioned

Answer: d
Clarification: None.

3. Which of these classes are used by character streams output operations?
a) InputStream
b) Writer
c) ReadStream
d) InputOutputStream

Answer: b
Clarification: Character streams uses Writer and Reader classes for input & output operations.

4. Which of these class is used to read from a file?
a) InputStream
b) BufferedInputStream
c) FileInputStream
d) BufferedFileInputStream

Answer: c
Clarification: None.

5. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String a="hello i love java";
  6.             System.out.println(indexof('i')+" "+indexof('o')+" "+lastIndexof('i')+" "+lastIndexof('o') ));
  7.         }
  8.     }

a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9

Answer: a
Clarification: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:

$ javac output.java
$ java output
6 4 6 9

6. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char c[]={'a','1','b',' ','A','0'];
  6.             for (int i = 0; i < 5; ++i)
  7.             {
  8. 	        if(Character.isDigit(c[i]))
  9.                     System.out.println(c[i]" is a digit");
  10.                 if(Character.isWhitespace(c[i]))
  11.                    System.out.println(c[i]" is a Whitespace character");
  12.                 if(Character.isUpperCase(c[i]))
  13.                    System.out.println(c[i]" is an Upper case Letter");
  14.                 if(Character.isUpperCase(c[i]))
  15.                    System.out.println(c[i]" is a lower case Letter");
  16.                 i = i + 3;
  17.             }
  18.         }
  19.     }

a)

   a is a lower case Letter
     is White space character

b)

   b is a lower case Letter
     is White space characte

c)

   a is a lower case Letter
   A is a upper case Letter

d)

   a is a lower case Letter
   0 is a digit

Answer: a
Clarification: Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang
they are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:

$ javac output.java
$ java output  
a is a lower case Letter
  is White space character

7. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            StringBuffer s2 = s1.reverse();
  7.            System.out.println(s2);
  8.         }
  9.     }

a) Hello
b) olleH
c) HelloolleH
d) olleHHello

Answer: b
Clarification: reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:

$ javac output.java
$ java output
olleH