OCA Java SE 8 Mock Exam Review - OCA Mock Question 31


What is the output of the following code?

public class Main {
  public static void main(String args[]) {
    int[] arr = new int[5];
    byte b = 4;
    char c = 'c';
    long longVar = 10;
    arr[0] = b;
    arr[1] = c;
    arr[3] = longVar;
    System.out.println(arr[0] + arr[1] + arr[2] + arr[3]);

a  4c010 
b  4c10 
c  113 
d  1034c 
e  Compilation error 




The previous code won't compile due to the following line of code:

arr[3] = longVar; 

This line of code tries to assign a value of type long to a variable of type int.

Assigning a char value to an int array element (arr[1] = c) is OK.

Adding a byte value to an int array element (arr[0] = b) is OK.

An unassigned int array element is assigned a default value (arr[2])

arr[0] + arr[1] + arr[2] + arr[3] prints the sum of all these values, or a concatenated value.