Variable Types


  1. What is the output of this program?

    public class dynamic_initialize
    {
    public static void main(String args[])
    {
    double p, q;
    p = 4.5;
    q = 10.0;
    double r = Math.sqrt(p * p + q * q);
    System.out.println(r);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

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


  1. Which of these is incorrect string literal?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    all string literals must begin and end in same line.



  1. What is the output of this program?

    public class scope_var
    {
    public static void main(String args[])
    {
    int a;
    a = 5;
    {
    int b = 6;
    System.out.print(a + " " + b);
    }
    System.out.println(a + " " + b);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A


    scope_var.java:11: error: cannot find symbol
    System.out.println(a + " " + b);
    ^
    symbol: variable b
    location: class scope_var
    1 error


  1. What is the output of this program?

    public class arr_var
    {
    public static void main(String args[])
    {
    int array_var [] = new int[20];
    for (int i = 0; i < 20; ++i) {
    array_var[i] = i/2;
    array_var[i]++;
    System.out.print(array_var[i] + " ");
    i++;
    }

    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    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: 1 2 3 4 5 6 7 8 9 10



  1. What is the output of this program?

    public class output
    {
    public static void main(String args[])
    {
    int p[] = {5,6,7,8,9};
    int q[] = p;
    int sum = 0;
    for (int j = 0; j < 4; ++j)
    sum += (p[j] * q[j + 1]) + (p[j + 1] * q[j]);
    System.out.println(sum);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    400