Java Identifier Quiz

Java Identifier Quiz | 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


Ans:- c) Digit

Q2) Java identifiers can’t contain?

a) _
b) @
c) $
d) All of these


Ans:- b) @

Q3) Which of the follwing can’t be used as Java identifier?

a) String
b) Int
c) main
d) long


Ans:- d) long
The long is a keyword.

Q4) Find invalid Java identifier?

a) hello world
b) hello_world
c) hello$world
d) helloWorld


Ans:- a) hello world
Space 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


Ans:- b) 10
Java 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


Ans:- d) Compile-time error
Duplicate variables are not allowed in Java.

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


Ans:- b) 6
There 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

Ans:- b) 9 Hi

In 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


Ans:- a) String

We 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

b) Compile-time error; 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

Ans:- _, enum, 43abc, true, static

Note:- It is suggested that use the names in your Java program which makes sense, above valid identifiers are given only for practice purpose, actually they don’t make sense in java programs. Therefore, never use them.

Please write comments if you find anything incorrect, or you want to share more information about Java Identifier Quiz.


Leave a Reply