2.8. Sample exam questions

Q2-1.

Given:

int myChar = 97;
int yourChar = 98;
System.out.print((char)myChar + (char)yourChar);

int age = 20;
System.out.print(" ");
System.out.print((float)age);

What is the output?

  1. 195 20.0
  2. 195 20
  3. ab 20.0
  4. ab 20
  5. Compilation error
  6. Runtime exception

Q2-2.

Which of the options are correct for the following code?

public class Prim {                                // line 1
    public static void main(String[] args) {       // line 2
        char a = 'a';                              // line 3
        char b = -10;                              // line 4
        char c = '1';                              // line 5
        integer d = 1000;                          // line 6
        System.out.println(++a + b++ * c - d);     // line 7
    }                                              // line 8
}                                                  // line 9

  1. Code at line 4 fails to compile.
  2. Code at line 5 fails to compile.
  3. Code at line 6 fails to compile.
  4. Code at line 7 fails to compile.

Q2-3.

What is the output of the following code?

public class Foo {
    public static void main(String[] args) {
        int a = 10;
        long b = 20;
        short c = 30;
        System.out.println(++a + b++ * c);
    }
}

  1. 611
  2. 641
  3. 930
  4. 960

Q2-4.

Given:

Boolean buy = new Boolean(true);
Boolean sell = new Boolean(true);

System.out.print(buy == sell);
boolean buyPrim = buy.booleanValue();
System.out.print(!buyPrim);

System.out.print(buy && sell);

What is the output?

  1. falsefalsefalse
  2. truefalsetrue
  3. falsetruetrue
  4. falsefalsetrue
  5. Compilation error
  6. Runtime exception

Q2-5.

Which of the following options contain correct code to declare and initialize variables to store whole numbers?

  1. bit a = 0;
  2. integer a2 = 7;
  3. long a3 = 0x10C;
  4. short a4 = 0512;
  5. double a5 = 10;
  6. byte a7 = -0;
  7. long a8 = 123456789;

Q2-6.

Select the options that, when inserted at // INSERT CODE HERE, will make the following code output a value of 11:

public class IncrementNum {
    public static void main(String[] args) {
        int ctr = 50;
        // INSERT CODE HERE
        System.out.println(ctr % 20);
    }
}

  1. ctr += 1;
  2. ctr =+ 1;
  3. ++ctr;
  4. ctr = 1;

Q2-7.

What is the output of the following code?

int a = 10;
int b = 20;
int c = (a * (b + 2)) - 10-4 * ((2*2) - 6;
System.out.println(c);

  1. 218
  2. 232
  3. 246
  4. Compilation error

Q2-8.

What is true about the following lines of code?

boolean b = false;
int i = 90;
System.out.println(i >= b);

  1. Code prints true
  2. Code prints false
  3. Code prints 90 >= false
  4. Compilation error

Q2-9.

Examine the following code and select the correct options:

public class Prim {                                          // line 1
    public static void main(String[] args) {                 // line 2
        int num1 = 12;                                       // line 3
        float num2 = 17.8f;                                  // line 4
        boolean eJavaResult = true;                          // line 5
        boolean returnVal = num1 >= 12 && num2 < 4.567       // line 6
                             || eJavaResult == true;
        System.out.println(returnVal);                       // line 7
    }                                                        // line 8
}                                                            // line 9

  1. Code prints false
  2. Code prints true
  3. Code will print true if code on line 6 is modified to the following:

    boolean returnVal = (num1 >= 12 && num2 < 4.567) || eJavaResult == true;
  4. Code will print true if code on line 6 is modified to the following:

    boolean returnVal = num1 >= 12 && (num2 < 4.567 || eJavaResult == false);

Q2-10.

Given:

boolean myBool = false;                               // line 1
int yourInt = 10;                                     // line 2
float hisFloat = 19.54f;                              // line 3
System.out.println(hisFloat = yourInt);               // line 4
System.out.println(yourInt > 10);                     // line 5
System.out.println(myBool = false);                   // line 6

What is the result?


  1. true
    true
    false

  2. 10.0
    false
    false

  3. false
    false
    false
  4. Compilation error

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.11