Question on Literals & Variables



Question on Literals & Variables

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

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?
    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);
        }
    }
a) 38
b) 39
c) 40
d) 41

Answer: c
Explanation: None.
output:
$ javac evaluate.java
$ java evaluate
40

7. 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++;
            }

        }
    }
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.
output:
$ javac array_output.java
$ java array_output
1 2 3 4 5

8. What is the output of this program?
    class variable_scope
    {
        public static void main(String args[])
        {
            int x;
            x = 5;
            {
                    int y = 6;
                    System.out.print(x + " " + y);
            }
            System.out.println(x + " " + y);
        }
    }
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.
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"

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

10. 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);
        }
    }
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.
output:
$ javac dynamic_initialization.java
$ java dynamic_initialization
5.0
Question on Literals & Variables Question on Literals & Variables Reviewed by Unknown on February 24, 2019 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.