Literals & Variables(Computer Science > Java Program ) Questions and Answers

Question 1.


What is the output of this program?


class dynamic_initialization {
public static void main(String args[])
{
double a, b;
a = 3.0;
b = 4.0;
double c = Math.sqrt(a * a + b * b);
System.out.println(c);
}
}
  1.    5.0
  2.    25.0
  3.    7.0
  4.    Compilation Error
Explanation:-
Answer: Option A. -> 5.0

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



Question 2.

Which of these is incorrect string literal?
A. "Hello World"
B. "Hello`setminus`nWorld"
C. "`setminus`"Hello World`setminus`""
D. "Hello
world"


  1.    5.0
  2.    25.0
  3.    7.0
  4.    Compilation Error
Explanation:-
Answer: Option A. -> 5.0

Answer: D
Explanation:all string literals must begin and end in same line.



Question 3.


What is the output of this program?


class array_output {
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
  1.    0 2 4 6 8
  2.    1 2 3 4 5
  3.    0 1 2 3 4 5 6 7 8 9
  4.    1 2 3 4 5 6 7 8 9 10
Explanation:-
Answer: Option B. -> 1 2 3 4 5

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



Question 4.


What is the output of this program?


class evaluate {
public static void main(String args[])
{
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
System.out.println(sum);
}
}
  1.    38
  2.    39
  3.    40
  4.    41
Explanation:-
Answer: Option C. -> 40

None
output:
$ javac evaluate.java
$ java evaluate
40



Question 5.

Which of these can not be used for a variable name in Java?


  1.    identifier
  2.    keyword
  3.    identifier & keyword
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> keyword

Keywords are specially reserved words which can not be used for naming a user defined 

variable, example : class, int, for etc.



Question 6.

Literal can be of which of these data types?


  1.    integer
  2.    float
  3.    boolean
  4.    all of the mentioned
Explanation:-
Answer: Option D. -> all of the mentioned

None.



Question 7.

Which of these can be returned by the operator & ?


  1.    Integer
  2.    Boolean
  3.    Character
  4.    Integer or Boolean
Explanation:-
Answer: Option D. -> Integer or Boolean

We can use binary ampersand operator on integers/chars (and it returns an integer) or on 

booleans (and it returns a boolean).



Question 8.

Literals in java must be appended by which of these?


  1.    L
  2.    l
  3.    D
  4.    L and I
Explanation:-
Answer: Option D. -> L and I

Data type long literals are appended by an upper or lowercase L.



Question 9.

Which of these is data type long literal?


  1.    0x99fffL
  2.    ABCDEFG
  3.    0x99fffa
  4.    99671246
Explanation:-
Answer: Option A. -> 0x99fffL

Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal 

long literal.