Java Programming MCQ (Multiple Choice
Questions)
1. Who invented Java Programming?
a) Guido van Rossum
b) James Gosling
c) Dennis Ritchie
d) Bjarne Stroustrup
Answer: b
Explanation: Java programming was developed by James Gosling at Sun Microsystems in 1995.
James Gosling is well known as the father of Java.
2. Which statement is true about Java?
a) Java is a sequence-dependent programming language
b) Java is a code dependent programming language
c) Java is a platform-dependent programming language
d) Java is a platform-independent programming language
Answer: d
Explanation: Java is called ‘Platform Independent Language’ as it primarily works on the principle
of ‘compile once, run everywhere’.
3. Which component is used to compile, debug and execute the java programs?
a) JRE
b) JIT
c) JDK
d) JVM
Answer: c
Explanation: 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.
4. Which one of the following is not a Java feature?
a) Object-oriented
b) Use of pointers
c) Portable
d) Dynamic and Extensible
Answer: b
Explanation: Pointers is not a Java feature. Java provides an e cient abstraction layer for
developing without using a pointer in Java. Features of Java Programming are Portable,
Architectural Neutral, Object-Oriented, Robust, Secure, Dynamic and Extensible, etc.
5. Which of these cannot be used for a variable name in Java?
a) identi er & keyword
ffi
b) identi er
c) keyword
fi
fi
d) none of the mentioned
fi
Answer: c
Explanation: Keywords are specially reserved words that can not be used for naming a user-
de ned variable, for example: class, int, for, etc.
fi
6. What is the extension of java code les?
a) .js
b) .txt
c) .class
d) .java
Answer: d
Explanation: Java les have .java extension.
7. What will be the output of the following Java code?
1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. [Link](++g * 8);
6. }
7. }
a) 32
b) 33
c) 24
d) 25
Answer: a
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by
8 gives 32.
output:
fi
$ javac [Link]
$ java increment
32
8. Which environment variable is used to set the java path?
a) MAVEN_Path
b) JavaPATH
c) JAVA
d) JAVA_HOME
Answer: d
Explanation: JAVA_HOME is used to store a path to the java installation.
9. What will be the output of the following Java program?
1. class output {
2. public static void main(String args[])
3. {
4. double a, b,c;
5. a = 3.0/0;
6. b = 0/4.0;
7. c=0/0.0;
8.
9. [Link](a);
10. [Link](b);
11. [Link](c);
12. }
13. }
a) NaN
b) In nity
c) 0.0
d) all of the mentioned
Answer: d
Explanation: For oating point literals, we have constant value to represent (10/0.0) in nity either
positive or negative and also have NaN (not a number for unde ned like 0/0.0), but for the
integral type, we don’t have any constant that’s why we get an arithmetic exception.
10. Which of the following is not an OOPS concept in Java?
a) Polymorphism
b) Inheritance
fi
c) Compilation
d) Encapsulation
Answer: c
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and
Abstraction.
11. What is not the use of “this” keyword in Java?
a) Referring to the instance variable when a local variable has the same name
b) Passing itself to the method of the same class
c) Passing itself to another method
d) Calling another constructor in constructor chaining
Answer: b
Explanation: “this” is an important keyword in java. It helps to distinguish between local variable
and variables passed in the method as parameters.
12. What will be the output of the following Java program?
1. class variable_scope
2. {
3. public static void main(String args[])
4. {
5. int x;
6. x = 5;
7. {
8. int y = 6;
9. [Link](x + " " + y);
10. }
11. [Link](x + " " + y);
12. }
13. }
a) Compilation error
b) Runtime error
c) 5 6 5 6
d) 5 6 5
Answer: a
Explanation: Second print statement doesn’t have access to y , scope y was limited to the block
de ned after initialization of x.
output:
$ javac variable_scope.java
Exception in thread "main" [Link]: Unresolved compilation problem: y cannot be resolved to a
13. What will be the error in the following Java code?
byte b = 50;
b = b * 50;
a) b cannot contain value 50
b) b cannot contain value 100, limited by its range
c) No error in this code
d) * operator has converted b * 50 into int, which can not be converted to byte without casting
Answer: d
Explanation: While evaluating an expression containing int, bytes or shorts, the whole expression
is converted to int then evaluated and the result is also of type int.
14. Which of the following is a type of polymorphism in Java Programming?
a) Multiple polymorphism
b) Compile time polymorphism
c) Multilevel polymorphism
d) Execution time polymorphism
Answer: b
Explanation: There are two types of polymorphism in Java. Compile time polymorphism
(overloading) and runtime polymorphism (overriding).
15. What will be the output of the following Java program?
1. class leftshift_operator
2. {
3. public static void main(String args[])
4. {
5. byte x = 64;
6. int i;
7. byte y;
8. i = x << 2;
9. y = (byte) (x << 2);
10. [Link](i + " " + y);
11. }
12. }
a) 0 256
b) 0 64
c) 256 0
d) 64 0
Answer: c
Explanation: None.
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0
16. What will be the output of the following Java code?
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. }
7. class main
8. {
9. public static void main(String args[])
10. {
11. box obj = new box();
12. [Link] = 10;
13. [Link] = 2;
14. [Link] = 10;
15. int y = [Link] * [Link] * [Link];
16. [Link](y);
17. }
18. }
a) 100
b) 400
c) 200
d) 12
Answer: c
Explanation: None.
output:
$ javac [Link]
$ java main
200
17. What is Truncation in Java?
a) Floating-point value assigned to a Floating type
b) Floating-point value assigned to an integer type
c) Integer value assigned to oating type
d) Integer value assigned to oating type
Answer: b
Explanation: None.
18. What will be the output of the following Java program?
1. class Output
2. {
3. public static void main(String args[])
4. {
5. int arr[] = {1, 2, 3, 4, 5};
6. for ( int i = 0; i < [Link] - 2; ++i)
7. [Link](arr[i] + " ");
8. }
9. }
a) 1 2 3 4 5
b) 1 2 3 4
c) 1 2
d) 1 2 3
Answer: d
Explanation: [Link]() is 5, so the loop is executed for three times.
output:
$ javac [Link]
$ java Output
1 2 3
19. What will be the output of the following Java code snippet?
1. class abc
2. {
3. public static void main(String args[])
4. {
5. if([Link]>0)
6. [Link]([Link]);
7. }
8. }
a) The snippet compiles and runs but does not print anything
b) The snippet compiles, runs and prints 0
c) The snippet compiles, runs and prints 1
d) The snippet does not compile
Answer: a
Explanation: As no argument is passed to the code, the length of args is 0. So the code will not
print.
20. What is the extension of compiled java classes?
a) .txt
b) .js
c) .class
d) .java
Answer: c
Explanation: The compiled java les have .class extension.
21. Which exception is thrown when java is out of memory?
a) MemoryError
b) OutOfMemoryError
c) MemoryOutOfBoundsException
d) MemoryFullException
Answer: b
Explanation: The Xms ag has no default value, and Xmx typically has a default value of 256MB. A
common use for these ags is when you encounter a [Link].
22. What will be the output of the following Java code?
1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. char chars[] = {'a', 'b', 'c'};
6. String s = new String(chars);
7. [Link](s);
8. }
9. }
a) abc
b) a
c) b
d) c
Answer: a
Explanation: String(chars) is a constructor of class string, it initializes string s with the values
stored in character array chars, therefore s contains “abc”.
23. Which of these are selection statements in Java?
a) break
b) continue
c) for()
d) if()
Answer: d
Explanation: Continue and break are jump statements, and for is a looping statement.
24. What will be the output of the following Java program?
1. class recursion
2. {
3. int func (int n)
4. {
5. int result;
6. if (n == 1)
7. return 1;
8. result = func (n - 1);
9. return result;
10. }
11. }
12. class Output
13. {
14. public static void main(String args[])
15. {
16. recursion obj = new recursion() ;
17. [Link]([Link](5));
18. }
19. }
a) 1
b) 120
c) 0
d) None of the mentioned
Answer: a
Explanation: None.
Output:
$ javac [Link]
$ java Output
1
25. What will be the output of the following Java code?
1. class output
2. {
3. public static void main(String args[])
4. {
5. String c = "Hello i love java";
6. boolean var;
7. var = [Link]("hello");
8. [Link](var);
9. }
10. }
a) 0
b) true
c) 1
d) false
Answer: d
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated di erently,
hence false is stored in var.
Output:
false
26. Which of these keywords is used to de ne interfaces in Java?
a) intf
b) Intf
c) interface
d) Interface
Answer: c
Explanation: interface keyword is used to de ne interfaces in Java.
27. 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("Quiz");
6. StringBuffer s2 = [Link]();
7. [Link](s2);
8. }
9. }
a) QuizziuQ
b) ziuQQuiz
c) Quiz
d) ziuQ
Answer: d
Explanation: reverse() method reverses all characters. It returns the reversed object on which it
was called.
Output:
$ javac [Link]
$ java output
ziuQ
28. What will be the output of the following Java code?
1. class Output
2. {
3. public static void main(String args[])
4. {
5. Integer i = new Integer(257);
6. byte x = [Link]();
7. [Link](x);
8. }
9. }
a) 257
b) 256
c) 1
d) 0
Answer: c
Explanation: [Link]() method returns the value of wrapper i as a byte value. i is 257, range of
byte is 256 therefore i value exceeds byte range by 1 hence 1 is returned and stored in x.
Output:
$ javac [Link]
$ java Output
1
29. What will be the output of the following Java program?
1. class Output
2. {
3. public static void main(String args[])
4. {
5. double x = 2.0;
6. double y = 3.0;
7. double z = [Link]( x, y );
8. [Link](z);
9. }
10. }
a) 9.0
b) 8.0
c) 4.0
d) 2.0
Answer: b
Explanation: [Link](x, y) methods returns value of y to the power x, i:e x ^ y, 2.0 ^ 3.0 = 8.0.
Output:
$ javac [Link]
$ java Output
8.0
30. Which of the following is a superclass of every class in Java?
a) ArrayList
b) Abstract class
c) Object class
d) String
Answer: c
Explanation: Object class is superclass of every class in Java.
31. What will be the output of the following Java code?
1. class Output
2. {
3. public static void main(String args[])
4. {
5. double x = 3.14;
6. int y = (int) [Link](x);
7. [Link](y);
8. }
9. }
a) 3
b) 0
c) 4
d) 3.0
Answer: c
Explanation: ciel(double X) returns the smallest whole number greater than or equal to variable x.
Output:
$ javac [Link]
$ java Output
4
32. What will be the output of the following Java program?
1. import [Link].*;
2. class networking
3. {
4. public static void main(String[] args) throws Exception
5. {
6. URL obj = new URL("[Link]
7. URLConnection obj1 = [Link]();
8. int len = [Link]();
9. [Link](len);
10. }
11. }
Note: Host URL is having length of content 127.
a) 127
b) 126
c) Runtime Error
d) Compilation Error
Answer: a
Explanation: None.
Output:
$ javac [Link]
$ java networking
127
33. Which of the below is not a Java Pro ler?
a) JPro ler
b) Eclipse Pro ler
c) JVM
d) JConsole
Answer: c
Explanation: Memory leak is like holding a strong reference to an object although it would never
be needed anymore. Objects that are reachable but not live are considered memory leaks.
Various tools help us to identify memory leaks.
34. What will be the output of the following Java program?
1. import [Link].*;
2. class networking
3. {
4. public static void main(String[] args) throws MalformedURLException
5. {
6. URL obj = new URL("[Link]
7. [Link]([Link]());
8. }
9. }
a) [Link]
b) [Link]
c) sanfoundry
d) [Link]
Answer: b
Explanation: toExternalForm() is used to know the full URL of an URL object.
Output:
$ javac [Link]
$ java networking
[Link]
35. What will be the output of the following Java code snippet?
1. import [Link].*;
2. class Arraylists
3. {
4. public static void main(String args[])
5. {
6. ArrayLists obj = new ArrayLists();
7. [Link]("A");
8. [Link]("B");
9. [Link]("C");
10. [Link](1, "D");
11. [Link](obj);
12. }
13. }
a) [A, D, C]
b) [A, B, C]
c) [A, B, C, D]
d) [A, D, B, C]
Answer: d
Explanation: obj is an object of class ArrayLists hence it is an dynamic array which can increase
and decrease its size. [Link](“X”) adds to the array element X and [Link](1,”X”) adds element x
at index position 1 in the list, Hence [Link](1,”D”) stores D at index position 1 of obj and shifts
the previous value stored at that position by 1.
Output:
$ javac [Link]
$ java Arraylist
[A, D, B, C].
36. Which of these packages contains the exception Stack Over ow in Java?
a) [Link]
b) [Link]
c) [Link]
d) [Link]
Answer: c
Explanation: None.
37. What will be the output of the following Java program?
1. import [Link].*;
2. class Collection_iterators
3. {
4. public static void main(String args[])
5. {
6. LinkedList list = new LinkedList();
7. [Link](new Integer(2));
8. [Link](new Integer(8));
9. [Link](new Integer(5));
10. [Link](new Integer(1));
11. Iterator i = [Link]();
12. [Link](list);
13. [Link](list);
14. while([Link]())
15. [Link]([Link]() + " ");
16. }
17. }
a) 1 2 5 8
b) 2 1 8 5
c) 1 5 8 2
d) 2 8 5 1
Answer: a
Explanation: [Link](list) sorts the given list, the list was 2->8->5->1 after sorting it
became 1->2->5->8.
Output:
1258
38. Which of these statements is incorrect about Thread?
a) start() method is used to begin execution of the thread
b) run() method is used to begin execution of a thread before start() method in special cases
c) A thread can be formed by implementing Runnable interface only
d) A thread can be formed by a class that extends Thread class
Answer: b
Explanation: run() method is used to de ne the code that constitutes the new thread, it contains
the code to be executed. start() method is used to begin execution of the thread that is execution
of run(). run() itself is never used for starting execution of the thread.
39. Which of these keywords are used for the block to be examined for exceptions?
a) check
b) throw
c) catch
d) try
Answer: d
Explanation: try is used for the block that needs to checked for exception.
40. What will be the output of the following Java code?
1. class newthread extends Thread
2. {
3. Thread t;
4. newthread()
5. {
6. t1 = new Thread(this,"Thread_1");
7. t2 = new Thread(this,"Thread_2");
8. [Link]();
9. [Link]();
10. }
11. public void run()
12. {
13. [Link](Thread.MAX_PRIORITY);
14. [Link]([Link](t2));
15. }
16. }
17. class multithreaded_programing
18. {
19. public static void main(String args[])
20. {
21. new newthread();
22. }
23. }
a) truetrue
b) falsefalse
c) true
d) false
Answer: b
Explanation: This program was previously done by using Runnable interface, here we have used
Thread class. This shows both the method are equivalent, we can use any of them to create a
thread.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse
41. Which one of the following is not an access modi er?
a) Protected
b) Void
c) Public
d) Private
Answer: b
Explanation: Public, private, protected and default are the access modi ers.
42. What will be the output of the following Java program?
1. final class A
2. {
3. int i;
4. }
5. class B extends A
6. {
7. int j;
8. [Link](j + " " + i);
9. }
10. class inheritance
11. {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. [Link]();
16. }
17. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: class A has been declared nal hence it cannot be inherited by any other class.
Hence class B does not have member i, giving compilation error.
output:
$ javac [Link]
Exception in thread "main" [Link]: Unresolved compilation problem:
i cannot be resolved or is not a field
43. What is the numerical range of a char data type in Java?
a) 0 to 256
b) -128 to 127
c) 0 to 65535
d) 0 to 32767
Answer: c
Explanation: Char occupies 16-bit in memory, so it supports 216 i:e from 0 to 65535.
44. Which class provides system independent server side implementation?
a) Server
b) ServerReader
c) Socket
d) ServerSocket
Answer: d
Explanation: ServerSocket is a [Link] class which provides system independent implementation
of server side socket connection.
45. What will be the output of the following Java program?
1. class overload
2. {
3. int x;
4. double y;
5. void add(int a , int b)
6. {
7. x = a + b;
8. }
9. void add(double c , double d)
10. {
11. y = c + d;
12. }
13. overload()
14. {
15. this.x = 0;
16. this.y = 0;
17. }
18. }
19. class Overload_methods
20. {
21. public static void main(String args[])
22. {
23. overload obj = new overload();
24. int a = 2;
25. double b = 3.2;
26. [Link](a, a);
27. [Link](b, b);
28. [Link](obj.x + " " + obj.y);
29. }
30. }
a) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6
Answer: a
Explanation: For [Link](a,a); ,the function in line number 4 gets executed and value of x is 4. For
the next function call, the function in line number 7 gets executed and value of y is 6.4
output:
$ javac Overload_methods.java
$ java Overload_methods
4 6.4
46. Which of the following is true about servlets?
a) Servlets can use the full functionality of the Java class libraries
b) Servlets execute within the address space of web server, platform independent and uses the
functionality of java class libraries
c) Servlets execute within the address space of web server
d) Servlets are platform-independent because they are written in java
Answer: b
Explanation: Servlets execute within the address space of a web server. Since it is written in java it
is platform independent. The full functionality is available through libraries.