Java MCQ (Multiple Choice Questions) - Sanfoundry
Java MCQ (Multiple Choice Questions) - Sanfoundry
View Answer
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.
ADVERTISEMENT
ADVERTISEMENT
View Answer
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
View Answer
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.
View Answer
Answer: b
Explanation: Pointers is not a Java feature. Java provides an efficient 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.
View Answer
Answer: c
Explanation: Keywords are specially reserved words that can not be used for naming a user-defined variable, for example: class, int, for, etc.
advertisement
6. What is the extension of java code files?
a) .js
b) .txt
c) .class
d) .java
View Answer
Answer: d
Explanation: Java files have .java extension.
1. class increment {
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }
a) 32
b) 33
c) 24
d) 25
View Answer
Answer: a
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:
$ javac increment.java
$ java increment
32
View Answer
Answer: d
Explanation: JAVA_HOME is used to store a path to the java installation.
1. class output {
3. {
4. double a, b,c;
5. a = 3.0/0;
6. b = 0/4.0;
7. c=0/0.0;
8.
9. System.out.println(a);
10. System.out.println(b);
11. System.out.println(c);
12. }
13. }
a) NaN
b) Infinity
c) 0.0
d) all of the mentioned
View Answer
Answer: d
Explanation: For floating point literals, we have constant value to represent (10/0.0) infinity either positive or negative and also have NaN (not a
number for undefined like 0/0.0), but for the integral type, we don’t have any constant that’s why we get an arithmetic exception.
advertisement
✕
10. Which of the following is not an OOPS concept in Java?
a) Polymorphism
b) Inheritance
c) Compilation
d) Encapsulation
View Answer
Answer: c
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and Abstraction.
View Answer
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.
1. class variable_scope
2. {
4. {
5. int x;
6. x = 5;
7. {
8. int y = 6;
10. }
12. }
13. }
a) Compilation error
b) Runtime error
c) 5 6 5 6
d) 5 6 5
View Answer
Answer: a
Explanation: Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.
output:
advertisement
$ javac variable_scope.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem: y cannot be resolved to a variable
byte b = 50;
b = b * 50;
View Answer
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.
View Answer
Answer: b
Explanation: There are two types of polymorphism in Java. Compile time polymorphism (overloading) and runtime polymorphism (overriding).
1. class leftshift_operator
2. {
4. {
5. byte x = 64;
6. int i;
7. byte y;
8. i = x << 2;
11. }
12. }
a) 0 256
b) 0 64
c) 256 0
d) 64 0
View Answer
Answer: c
Explanation: None.
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. }
7. class main
8. {
10. {
13. obj.height = 2;
16. System.out.print(y);
17. }
18. }
a) 100
b) 400
c) 200
d) 12
View Answer
Answer: c
Explanation: None.
output:
$ javac main.java
$ java main
200
View Answer
Answer: b
Explanation: None.
1. class Output
2. {
4. {
8. }
9. }
a) 1 2 3 4 5
b) 1 2 3 4
c) 1 2
d) 1 2 3
View Answer
Answer: d
Explanation: arr.length() is 5, so the loop is executed for three times.
output:
$ javac Output.java
$ java Output
123
19. What will be the output of the following Java code snippet?
1. class abc
2. {
4. {
5. if(args.length>0)
6. System.out.println(args.length);
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
View Answer
Answer: a
Explanation: As no argument is passed to the code, the length of args is 0. So the code will not print.
View Answer
Answer: c
Explanation: The compiled java files have .class extension.
View Answer
Answer: b
Explanation: The Xms flag has no default value, and Xmx typically has a default value of 256MB. A common use for these flags is when you
encounter a java.lang.OutOfMemoryError.
2. {
4. {
7. System.out.println(s);
8. }
9. }
a) abc
b) a
c) b
d) c
View Answer
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”.
View Answer
Answer: d
Explanation: Continue and break are jump statements, and for is a looping statement.
1. class recursion
2. {
4. {
5. int result;
6. if (n == 1)
7. return 1;
9. return result;
10. }
11. }
13. {
15. {
18. }
19. }
a) 1
b) 120
c) 0
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
Output:
$ javac Output.javac
$ java Output
1. class output
2. {
4. {
6. boolean var;
7. var = c.startsWith("hello");
8. System.out.println(var);
9. }
10. }
a) 0
b) true
c) 1
d) false
View Answer
Answer: d
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
Output:
false
View Answer
Answer: c
Explanation: interface keyword is used to define interfaces in Java.
1. class output
2. {
4. {
6. StringBuffer s2 = s1.reverse();
7. System.out.println(s2);
8. }
9. }
a) QuizziuQ
b) ziuQQuiz
c) Quiz
d) ziuQ
View Answer
Answer: d
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:
$ javac output.java
$ java output
ziuQ
1. class Output
2. {
4. {
6. byte x = i.byteValue();
7. System.out.print(x);
8. }
9. }
a) 257
b) 256
c) 1
d) 0
View Answer
Answer: c
Explanation: i.byteValue() 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 Output.java
$ java Output
1. class Output
2. {
4. {
5. double x = 2.0;
6. double y = 3.0;
7. double z = Math.pow( x, y );
8. System.out.print(z);
9. }
10. }
a) 9.0
b) 8.0
c) 4.0
d) 2.0
View Answer
Answer: b
Explanation: Math.pow(x, y) methods returns value of y to the power x, i:e x ^ y, 2.0 ^ 3.0 = 8.0.
Output:
$ javac Output.java
$ java Output
8.0
View Answer
Answer: c
Explanation: Object class is superclass of every class in Java.
2. {
4. {
5. double x = 3.14;
7. System.out.print(y);
8. }
9. }
a) 3
b) 0
c) 4
d) 3.0
View Answer
Answer: c
Explanation: ciel(double X) returns the smallest whole number greater than or equal to variable x.
Output:
$ javac Output.java
$ java Output
1. import java.net.*;
2. class networking
3. {
5. {
9. System.out.print(len);
10. }
11. }
View Answer
Answer: a
Explanation: None.
Output:
$ javac networking.java
$ java networking
127
View Answer
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.
1. import java.net.*;
2. class networking
3. {
5. {
7. System.out.print(obj.toExternalForm());
8. }
9. }
a) www.sanfoundry.com
b) https://www.sanfoundry.com/javamcq
c) sanfoundry
d) sanfoundry.com
View Answer
Answer: b
Explanation: toExternalForm() is used to know the full URL of an URL object.
Output:
$ javac networking.java
$ java networking
https://www.sanfoundry.com/javamcq
35. What will be the output of the following Java code snippet?
1. import java.util.*;
2. class Arraylists
3. {
4. public static void main(String args[])
5. {
7. obj.add("A");
8. obj.add("B");
9. obj.add("C");
11. System.out.println(obj);
12. }
13. }
a) [A, D, C]
b) [A, B, C]
c) [A, B, C, D]
d) [A, D, B, C]
View Answer
Answer: d
Explanation: obj is an object of class ArrayLists hence it is an dynamic array which can increase and decrease its size. obj.add(“X”) adds to the
array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and shifts
the previous value stored at that position by 1.
Output:
$ javac Arraylist.java
$ java Arraylist
[A, D, B, C].
36. Which of these packages contains the exception Stack Overflow in Java?
a) java.io
b) java.system
c) java.lang
d) java.util
View Answer
Answer: c
Explanation: None.
1. import java.util.*;
2. class Collection_iterators
3. {
5. {
7. list.add(new Integer(2));
8. list.add(new Integer(8));
9. list.add(new Integer(5));
10. list.add(new Integer(1));
12. Collections.reverse(list);
13. Collections.sort(list);
14. while(i.hasNext())
16. }
17. }
a) 1 2 5 8
b) 2 1 8 5
c) 1 5 8 2
d) 2 8 5 1
View Answer
Answer: a
Explanation: Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting it became 1->2->5->8.
Output:
1258
View Answer
Answer: b
Explanation: run() method is used to define 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
View Answer
Answer: d
Explanation: try is used for the block that needs to checked for exception.
2. {
3. Thread t;
4. newthread()
5. {
6. t1 = new Thread(this,"Thread_1");
7. t2 = new Thread(this,"Thread_2");
8. t1.start();
9. t2.start();
10. }
12. {
13. t2.setPriority(Thread.MAX_PRIORITY);
14. System.out.print(t1.equals(t2));
15. }
16. }
18. {
20. {
22. }
23. }
a) truetrue
b) falsefalse
c) true
d) false
View Answer
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
View Answer
Answer: b
Explanation: Public, private, protected and default are the access modifiers.
1. final class A
2. {
3. int i;
4. }
5. class B extends A
6. {
7. int j;
9. }
11. {
13. {
15. obj.display();
16. }
17. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer
Answer: d
Explanation: class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving
compilation error.
output:
$ javac inheritance.java
View Answer
Answer: c
Explanation: Char occupies 16-bit in memory, so it supports 216 i:e from 0 to 65535.
View Answer
Answer: d
Explanation: ServerSocket is a java.net 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;
6. {
7. x = a + b;
8. }
10. {
11. y = c + d;
12. }
13. overload()
14. {
15. this.x = 0;
16. this.y = 0;
17. }
18. }
20. {
22. {
24. int a = 2;
29. }
30. }
a) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6
View Answer
Answer: a
Explanation: For obj.add(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
View Answer
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.
Our 1000+ MCQs focus on all topics of the Java subject, covering 100+ topics. This will help you to prepare for exams,
contests, online tests, quizzes, viva-voce, interviews, and certifications. You can practice these MCQs chapter by
chapter starting from the 1st chapter or you can jump to any chapter of your choice. You can also download the
PDF of Java MCQs by applying below.
5. Java Inheritance
The section contains Java multiple choice questions and answers on integer, character, floating and boolean data types, variables, type casting and
conversions and properties of arrays.
Java Integer and Floating Data Types Java Data Type – Date & TimeZone
Java Character and Boolean Data Types Java Literals & Java Variables
The section contains Java questions and answers on arithmetic, bitwise, relational, boolean and assignment operators. The section also contains questions
on control statements.
Java Relational Operator and Boolean Logic Operators Java Control Statements – 2
The section contains Java MCQs on oops concepts, jdk, jre, jit and jvm.
The section contains Java multiple choice questions and answers on fundamentals of classes, methods basics, heap and garbage collection, object
creation, constructors, access control, string class, method overloading and static keyword, command line arguments and recursion.