Java Multiple Choice Questions & Answers on Literals & Variables for Freshers

1. Which of these is long data type literal?

a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
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
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
Answer:d

Explanation:Data type long literals are appended by an upper or lowercase L.
4. Literal can be of which of these data types?

a) integer
b) float
c) boolean
d) all of the mentioned
Answer:d
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
Answer:b

Explanation:Keywords are specially reserved words which can not be used for naming a user defined variable, example : class, int, for etc.
6. What is the output of this program?
  1.     class evaluate {
  2.         public static void main(String args[]) 
  3.         {
  4.             int a[] = {1,2,3,4,5};
  5.      int d[] = a;
  6.      int sum = 0;
  7.      for (int j = 0; j < 3; ++j)
  8.                 sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
  9.      System.out.println(sum);
  10.         } 
  11.     }
a) 38
b) 39
c) 40
d) 41
Answer:c
7. What is the output of this program?
  1.     class array_output {
  2.         public static void main(String args[]) 
  3.         {
  4.             int array_variable [] = new int[10];
  5.      for (int i = 0; i < 10; ++i) {
  6.                 array_variable[i] = i/2;
  7.                 array_variable[i]++;
  8.                 System.out.print(array_variable[i] + " ");
  9.                 i++;
  10.             }
  11.  
  12.         } 
  13.     }
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
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.
8. What is the output of this program?
  1.     class variable_scope {
  2.         public static void main(String args[]) 
  3.         {
  4.             int x;
  5.             x = 5;
  6.             {
  7.          int y = 6;
  8.          System.out.print(x + " " + y);
  9.             }
  10.             System.out.println(x + " " + y);
  11.         } 
  12.     }
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
Answer:d

Explanation:Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.
9. Which of these is incorrect string literal?

a) “Hello World”
b) “Hello\nWorld”
c) “\”Hello World\””
d) “Hello
world”
Answer:d

Explanation:all string literals must begin and end in same line.
10. What is the output of this program?
  1.     class dynamic_initialization {
  2.         public static void main(String args[]) 
  3.         {
  4.             double a, b;
  5.             a = 3.0;
  6.             b = 4.0;
  7.      double c = Math.sqrt(a * a + b * b);
  8.      System.out.println(c);
  9.         } 
  10.     }
a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
Answer:a

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

Related

C# Questions & Answers on Introduction of Stream Classes for Freshers

1. Select the namespace on which the stream classes are defined? a) System.IOb) System.Inputc) System.Outputd) All of the mentioned Answer: a Explanation: The core stream classes are defined with...

Linux Questions & Answers on Shell Redirection for Freshers

1. The redirection 2> abc implies a) Write file 2 to file abcb) Write standard output to abcc) Write standard error to abcd) None of the mentioned Answer: c 2. cmd 2>&1 > abc will a...

C# Questions & Answers on Writing Console Output Operations for Freshers

1. Select the objects of the class TextWriter which is/are not used to perform the write operations to the console? a) Write()b) WriteLine()c) WriteError()d) All of the mentioned Answer: c Explan...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item