Multiple Choice Questions 7

  1. Which of the following statements is valid?

  2. a. int i = new int(30);
    b. double d[] = new double[30];
    c. int i[] = (3, 4, 3, 2);
    d. char[] c = new char();

  3. How can you initialize an array of two characters to 'a' and 'b'?

  4. a. char[] charArray = new char[2]; charArray = {'a', 'b'};
    b. char[2] charArray = {'a', 'b'};
    c. char[] charArray = {'a', 'b'};
    d. None of the above

  5. What is the representation of the third element in an array called a?

  6. a. a[2]
    b. a(2)
    c. a[3]
    d. a(3)

  7. What is the printout for the following code?
    class Test
    { public static void main(String[] args)
    { int[] x = new int[3];
    System.out.println("x[0] is "+x[0]);
    }
    }

  8. a. The program has a syntax error because the size of the array wasn't specified when declaring the array.
    b. The program has a runtime error because the array elements are not initialized.
    c. The program runs fine and displays 0.
    d. None of the above

  9. In the following code, what is the printout for list2?
    class Test
    { public static void main(String[] args)
    { int list1 = {1, 2, 3};
    int list2 = {1, 2, 3};
    list2 = list1;
    list1 = {1, 1, 1};
    for (int i=0; iSystem.out.print(list2[i]+" ");
    }
    }

  10. a. 1 2 3
    b. 1 1 1
    c. None of the above

  11. Suppose s is a string with the value "java". What will be assigned to x if you e
    execute the following code?
    char x = s.charAt(4);

  12. a. 'a'
    b. 'v'
    c. Nothing will be assigned to c, because the execution causes the runtime error StingIndexOutofBoundsException.

  13. What is the output of the following code?
    class Test
    { public static void main(String[] args)
    { String s1 = new String("Welcome to Java!");
    String s2 = s1;
    if (s1 == s2)
    System.out.println("s1 is equal to s2");
    else
    System.out.println("s1 is not equal to s2");
    }
    }

  14. a. s1 is equal to s2
    b. s1 is not equal to s2

  15. What is the output of the following code?
    class Test
    { public static void main(String[] args)
    { String s1 = new String("Welcome to Java!");
    String s2 = new String("Welcome to Java!");
    if (s1 == s2)
    System.out.println("s1 is equal to s2");
    else
    System.out.println("s1 is not equal to s2");
    }
    }

  16. a. s1 is equal to s2
    b. s1 is not equal to s2

  17. What is the output of the following code?
    class Test
    { public static void main(String[] args)
    { String s1 = new String("Welcome to Java!");
    String s2 = new String("Welcome to Java!");
    if (s1.equals(s2))
    System.out.println("s1 is equal to s2");
    else
    System.out.println("s1 is not equal to s2");
    }
    }

  18. a. s1 is equal to s2
    b. s1 is not equal to s2

  19. The statement What is the output of the following code?
    class Test
    { public static void main(String[] args)
    { String s1 = new String("Welcome to Java!");
    String s2 = s1.toUpperCase();
    if (s1 == s2)
    System.out.println("s1 is equal to s2");
    else
    System.out.println("s1 is not equal to s2");
    }
    }

  20. a. s1 is equal to s2
    b. s1 is not equal to s2

  21. What is the output of the following code?
    class Test
    { public static void main(String[] args)
    { String s1 = new String("Welcome to Java");
    String s2 = s1;
    s1 += "and Welcome to HTML";
    if (s1 == s2)
    System.out.println("s1 is equal to s2");
    else
    System.out.println("s1 is not equal to s2");
    }
    }

  22. a. s1 is equal to s2
    b. s1 is not equal to s2

  23. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

  24. a. String s = new String("new string");
    b. String s3 = s1 + s2;
    c. s1 >= s2
    d. int i = s1.length();

  25. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

  26. a. String s3 = s1 - s2;
    b. boolean b = s1.compareTo(s2);
    c. char c = s1[0];
    d. char c = s1.charAt(s1.length()-1);

  27. What is the output of the following program?
    import java.util.StringTokenizer;
    class TestStringTokenizer
    { public static void main(String[] args)
    { //create a string and string tokenizer
    String s = "I/am\learning Java.";
    StringTokenizer st = new StringTokenizer(s, "/\.");
    //retrieve and display tokens
    while (st.hasMoreTokens())
    System.out.print(st.nextToken()+" ");
    }
    }

  28. a. I am learning Java.
    b. I/am\learning Java.
    c. I am learning Java
    d. None of the above

  29. How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2

  30. a. args[0]
    b. args[1]
    c. args[2]
    d. args[3]

Visited times since 13 March 1999.