Java Questions & Answers – Literals & Variables

This section of our 1000+ Java MCQs focuses on literals & variables of Java Programming Language.

1. Which of these is long data type literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
View Answer

Answer: a
Explanation: Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal long literal.

2. Which of these can be returned by the operator &?
a) Integer
b) Boolean
c) Character
d) Integer or Boolean
View Answer

Answer: d
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it returns a boolean).

3. Literals in java must be appended by which of these?
a) L
b) l
c) D
d) L and I
View Answer

Answer: d
Explanation: Data type long literals are appended by an upper or lowercase L.
advertisement
advertisement

4. Literal can be of which of these data types?
a) integer
b) float
c) boolean
d) all of the mentioned
View Answer

Answer: d
Explanation: None

5. Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) none of the mentioned
View Answer

Answer: b
Explanation: Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. What will be the output of the following Java program?

  1.     class evaluate 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int a[] = {1,2,3,4,5};
  6. 	    int d[] = a;
  7. 	    int sum = 0;
  8. 	    for (int j = 0; j < 3; ++j)
  9.                 sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
  10. 	    System.out.println(sum);
  11.         } 
  12.     }

a) 38
b) 39
c) 40
d) 41
View Answer

Answer: c
Explanation: None.
output:

advertisement
$ javac evaluate.java
$ java evaluate
40

7. What will be the output of the following Java program?

advertisement
  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.        	    int array_variable [] = new int[10];
  6. 	    for (int i = 0; i < 10; ++i) {
  7.                 array_variable[i] = i/2;
  8.                 array_variable[i]++;
  9.                 System.out.print(array_variable[i] + " ");
  10.                 i++;
  11.             }
  12.  
  13.         } 
  14.     }

a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
View Answer

Answer: b
Explanation: When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
output:

$ javac array_output.java
$ java array_output
1 2 3 4 5

8. 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. 	        System.out.print(x + " " + y);
  10.             }
  11.             System.out.println(x + " " + y);
  12.         } 
  13.     }

a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
View Answer

Answer: d
Explanation: Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.
output:

$ javac variable_scope.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem: y cannot be resolved to a variable

9. Which of these is an incorrect string literal?
a) “Hello World”
b) “Hello\nWorld”
c) “\”Hello World\””
d)

"Hello
world"
View Answer
Answer: d
Explanation: All string literals must begin and end in the same line.
 
 

10. What will be the output of the following Java program?

  1.     class dynamic_initialization 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             double a, b;
  6.             a = 3.0;
  7.             b = 4.0;
  8. 	    double c = Math.sqrt(a * a + b * b);
  9. 	    System.out.println(c);
  10.         } 
  11.     }

a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
View Answer

Answer: a
Explanation: Variable c has been dynamically initialized to square root of a * a + b * b, during run time.
output:

$ javac dynamic_initialization.java
$ java dynamic_initialization
5.0
Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.