Java Identifier MCQ

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) Digit

Q2) 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) long

The 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 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

View Answer Answer:- 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

View Answer Answer:- d) Compile-time error

Duplicate 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) 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

View Answer Answer:- 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

View Answer Answer:- 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

View Answer Answer:- b) Compile-time error

In 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?

  1. provides
  2. _____
  3. extend
  4. shortint
  5. Know_Program
  6. _
  7. synchronize
  8. packages
  9. tries
  10. enum
  11. ____$$$
  12. _987
  13. main
  1. 43abc
  2. do_while
  3. Class
  4. Maker
  5. true
  6. _xyz
  7. static
  8. to
  9. String
  10. newnew
  11. annotation
  12. protect
View Answer Answer:- _, 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 purposes, actually they don’t make sense in java programs. Therefore, never use them.

Leave a Comment

Your email address will not be published. Required fields are marked *