Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Java Identifier MCQ | Find valid Java Identifier | Recommended reading:- Identifiers in Java
Q1) Java identifiers should not start with?
a) UpperCase Character
b) LowerCase Character
c) Digit
d) Underscore
View Answer
Answer:- c) DigitQ2) Java identifiers can’t contain?
a) _
b) @
c) $
d) All of these
View Answer
Answer:- b) @Q3) Which of the following can’t be used as a Java identifier?
a) String
b) Int
c) main
d) long
View Answer
Answer:- d) longThe long is a keyword, it can’t be used as an identifier.
Q4) Find an invalid Java identifier?
a) hello world
b) hello_world
c) hello$world
d) helloWorld
View Answer
Answer:- a) hello worldSpace is a special character, and Java identifier can’t contain any special symbol except _ and $.
Q5) Find the output of the below program?
public class Test {
public static void main(String[] args) {
int height = 9;
int Height = 10;
System.out.println(Height);
}
}
a) 9
b) 10
c) 19
d) Compile-time error
View Answer
Answer:- b) 10Java identifiers are case sensitive, and uppercase characters are different from lowercase characters. The variables of the above program ‘height’ and ‘Height’ are not same.
Q6) Find the output of the below program?
public class Test {
public static void main(String[] args) {
int a = 9;
int a = 10;
System.out.println(a);
}
}
a) 9
b) 10
c) 19
d) Compile-time error
View Answer
Answer:- d) Compile-time errorDuplicate variables are not allowed in Java. The “a” variable is declared twice in the above program.
Q7) What is the output of the below program?
public class Test {
public static void main(String[] args) {
int abcdefghijklmnopqrstuvwxyz012345 = 6;
System.out.println(abcdefghijklmnopqrstuvwxyz012345);
}
}
a) 0
b) 6
c) Compile-time error
d) Exception
View Answer
Answer:- b) 6There is no limitation on length of an identifier.
Quiz on the Pre-defined name as Java identifier
Q8) Find the output of the given program?
class Test{
public static void main(String[] args) {
int out=9;
System.out.print(out+" ");
println();
}
public static void println(){
System.out.println("Hi");
}
}
a) 9
b) 9 Hi
c) Compile-time error
d) Exception
View Answer
Answer:- b) 9 HiIn this program, we defined a variable “out” having integer value 9. We also used a user-defined method println(). Here out and println() are in the current program but the out and println() of System.out.println present in the class System and the System class is present in the java\lang folder. So, JVM treated System.out.println() as java.lang.System.out.println() and we don’t get any error.
Q9) Find the output of the given program?
class Test{
public static void main(String[] args) {
String String = "String";
System.out.println(String);
}
}
a) String
b) Compile-time error
c) Exception
d) None of these
View Answer
Answer:- a) StringWe created a variable “String” of class String having value “String” and it is valid.
Q10) Find the output of the given program?
class Test{
public static void main(String[] args) {
String System="Variable";
System.out.println(System);
}
}
a) Variable
b) Compile-time error
c) Exception
d) None of these
View Answer
Answer:- b) Compile-time errorIn this program, At compilation we get an error.
System is a pre-defined class and where ever we are calling System.out.println() then System class is used in the program. To display values to the console it should not be distrubed until we are not using another class to display the data.
In our program, the compiler and JVM treats System as a variable, not as a class. In, the 4th line Compiler didn’t found any out variable inside the System variable. So, it gives an error. Solution:- in the 4th line use java.lang.System.out.println(System);
Q11) Find illegal Java identifiers in the given list?
- provides
- _____
- extend
- shortint
- Know_Program
- _
- synchronize
- packages
- tries
- enum
- ____$$$
- _987
- main
- 43abc
- do_while
- Class
- Maker
- true
- _xyz
- static
- to
- String
- newnew
- annotation
- protect
View Answer
Answer:- _, enum, 43abc, true, staticNote:- It is suggested that use the names in your Java program which makes sense, above valid identifiers are given only for practice purposes, actually they don’t make sense in java programs. Therefore, never use them.