Classes/Objects  «Prev  Next»

Casting and converting - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 

1. Which answer is true about the following program?

public class Example {
  public static void main(String[] args) {
    byte b = 3;
    b = -b;
    System.out.println(b);
  }
} 


Please select the best answer.
  A. An error is generated when the program is compiled.
  B. The program displays the value -3.
  C. The program displays the value 253.
  D. The program generates a RuntimeException.

2. Which answer is true about the following program?

public class Example {
  public static void main(String[] args) {
    double d = 259;
    byte b = (byte)d;
    System.out.println(b);
  }
}
 

Please select the best answer.
  A. An error is generated when the program is compiled.
  B. The program displays the value 259.
  C. The program displays the value 3.
  D. The program generates a RuntimeException.

3. Which answers are true about the following program?

public class Example {
  public static void main(String[] args) {
    String s = "456";
    int i = 123;
    System.out.println((int)i + s);
  }
}

Please select all the correct answers.
  A. An error is generated when the program is compiled.
  B. The program displays the value 579.
  C. The program generates a RuntimeException.
  D. The program displays the value 123456.

4. Which answers are true about the following program?

public class Example {
  public static void main(String[] args) {
    String s = "123";
    Object o = s;
    s = (String)o;
    System.out.println(s);
  }
}

Please select all the correct answers.
  A. An error is generated when the program is compiled.
  B. The program displays the value String["123"].
  C. The program displays the value 123
  D. The program generates a RuntimeException.