Scjp 6.0 _ Java _ Level 1

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By LanFang
L
LanFang
Community Contributor
Quizzes Created: 2 | Total Attempts: 662
Questions: 10 | Attempts: 491

SettingsSettingsSettings
Java Quizzes & Trivia

歡迎來挑戰 SCJP 6.0_JAVA_ level 1


Questions and Answers
  • 1. 

    Given: public class TestString1 {     public static void main(String[] args) {         String str = "420";         str += 42;         System.out.print(str);     } } What is the output? 

    • A.

      42

    • B.

      420

    • C.

      462

    • D.

      42042

    • E.

      Compilation fails.

    • F.

      An exception is thrown at runtime.

    Correct Answer
    D. 42042
    Explanation
    The output of this code is "42042". The code starts with a string "420" and then uses the += operator to concatenate the number 42 to the string. This results in the final string "42042".

    Rate this question:

  • 2. 

    Given: 35. String #name = "Jane Doe"; 36. int $age = 24; 37. Double _height = 123.5; 38. double ~temp = 37.5;  Which two statements are true? (Choose two.) 

    • A.

      Line 35 will not compile.

    • B.

      Line 36 will not compile.

    • C.

      Line 37 will not compile.

    • D.

      Line 38 will not compile.

    Correct Answer(s)
    A. Line 35 will not compile.
    D. Line 38 will not compile.
    Explanation
    Line 35 will not compile because the variable name cannot start with a special character (#).

    Line 38 will not compile because the variable temp cannot start with a special character (~).

    Rate this question:

  • 3. 

    Given: public class Test {     public static void main(String[] args) {         int x = 5;         boolean b1 = true;         boolean b2 = false;         if ((x == 4) && !b2)             System.out.print("1 ");             System.out.print("2 ");         if ((b2 = true) && b1)             System.out.print("3 ");     } } What is the result? 

    • A.

      2

    • B.

      3

    • C.

      12

    • D.

      23

    • E.

      123

    • F.

      Compilation fails.

    • G.

      An exception is thrown at runtime.

    Correct Answer
    D. 23
    Explanation
    The code first checks if x is equal to 4 and b2 is false, which is false in this case. Therefore, the first if statement is not executed. Then, the code prints "2" because it is outside of the if statement. After that, the code assigns true to b2 and checks if b2 is true and b1 is true, which is true in this case. Therefore, the second if statement is executed and "3" is printed. So, the final output is "23".

    Rate this question:

  • 4. 

    Given:     public void go() {         String o = "";         z:         for (int x = 0; x < 3; x++) {             for (int y = 0; y < 2; y++) {                 if (x == 1)break;                 if (x == 2 && y == 1)break z;                 o = o + x + y;             }         }         System.out.println(o);     } What is the result when the go() method is invoked? 

    • A.

      00

    • B.

      0001

    • C.

      000120

    • D.

      00012021

    • E.

      Compilation fails.

    • F.

      An exception is thrown at runtime.

    Correct Answer
    C. 000120
    Explanation
    The go() method contains two nested for loops. The outer loop iterates from 0 to 2 and the inner loop iterates from 0 to 1. Inside the inner loop, there are two conditions. If x is equal to 1, the inner loop is terminated using the "break" statement. If x is equal to 2 and y is equal to 1, the outer loop is terminated using the "break z" statement. In all other cases, the value of x and y are concatenated to the string "o".

    Therefore, when the go() method is invoked, the value of "o" becomes "000120" and it is printed.

    Rate this question:

  • 5. 

    Given:     int x = 0;     int y = 10;     while (x < 5) {         y--;         ++x;     } ;     System.out.print(x + "," + y); What is the result? 

    • A.

      5,6

    • B.

      5,5

    • C.

      6,5

    • D.

      6,6

    Correct Answer
    B. 5,5
    Explanation
    The given code initializes x to 0 and y to 10. It then enters a while loop that continues as long as x is less than 5. Inside the loop, y is decremented by 1 and x is incremented by 1. This process continues until x becomes 5. After the loop, the values of x and y are printed, which are 5 and 5 respectively. Therefore, the correct answer is 5,5.

    Rate this question:

  • 6. 

    Given: public class Breaker2 {     static String o = "";     public static void main(String[] args) {         z: for (int x = 2; x < 7; x++) {             if (x == 3)                 continue;             if (x == 5)                 break z;             o = o + x;         }         System.out.println(o);     } } What is the result? 

    • A.

      2

    • B.

      24

    • C.

      234

    • D.

      246

    • E.

      2346

    • F.

      Compilation fails.

    Correct Answer
    B. 24
    Explanation
    The code uses a label "z" to create a labeled loop. The loop iterates from 2 to 6. If the value of x is 3, the continue statement is executed, which skips the current iteration. If the value of x is 5, the break statement with the label "z" is executed, which terminates the loop. Otherwise, the value of x is concatenated to the string "o". In this case, the loop will iterate from 2 to 6, excluding 3 and terminating at 5. Hence, the value of "o" will be "24".

    Rate this question:

  • 7. 

    Given:     String[] elements = {"for", "tea", "too"};     String first = (elements.length>0) ? elements[0] : null; What is the result? 

    • A.

      Compilation fails.

    • B.

      An exception is thrown at runtime.

    • C.

      The variable first is set to null.

    • D.

      The variable first is set to elements[0].

    Correct Answer
    D. The variable first is set to elements[0].
    Explanation
    The correct answer is "The variable first is set to elements[0]." In this code, the ternary operator is used to check if the length of the elements array is greater than 0. If it is, the value of the first element in the array (elements[0]) is assigned to the variable first. Otherwise, null is assigned to first. Since the length of the elements array is 3, the variable first will be set to the value of elements[0], which is "for".

    Rate this question:

  • 8. 

    Given: class Alligator {     public static void main(String[] args) {         int[] x[] = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };         int y[][] = x;         System.out.print(y[2][1]);     } } What is the result? 

    • A.

      2

    • B.

      3

    • C.

      4

    • D.

      6

    • E.

      7

    • F.

      Compilation fails.

    Correct Answer
    E. 7
    Explanation
    The code declares and initializes a 2D array called "x" with three rows and different number of columns in each row. Then, it declares another 2D array called "y" and assigns "x" to it. Finally, it prints the value at index [2][1] of array "y", which is 7. Therefore, the result is 7.

    Rate this question:

  • 9. 

    Given: public class Barn {     public static void main(String[] args) {         new Barn.go("hi", 1);         new Barn.go("hi", "world", 2);     }     public void go(String... y, int x) {         System.out.print(y[y.length - 1] + " ");     } } What is the result? 

    • A.

      Hi hi

    • B.

      Hi world

    • C.

      World world

    • D.

      Compilation fails.

    • E.

      An exception is thrown at runtime.

    Correct Answer
    D. Compilation fails.
    Explanation
    The code will not compile because the method go() is not static, but it is being called from the static main() method without creating an instance of the Barn class. The go() method should either be made static or an instance of the Barn class should be created before calling the go() method.

    Rate this question:

  • 10. 

    Given: 1. class Super{ 2.     private int a; 3.     protected Super(int a){this.a = a;} 4. } ... 11. class Sub extends Super{ 12.     public Sub(int a){super(a);} 13.     public Sub(){this.a = 5;} 14. } Which two, independently, will allow Sub to compile? (Choose two.) 

    • A.

      Change line 2 to:  public int a;

    • B.

      Change line 2 to:  protected int a;

    • C.

      Change line 13 to:  public Sub(){this(5);}

    • D.

      Change line 13 to:  public Sub(){super(5);}

    • E.

      Change line 13 to:  public Sub(){super(a);}

    Correct Answer(s)
    C. Change line 13 to:  public Sub(){this(5);}
    D. Change line 13 to:  public Sub(){super(5);}
    Explanation
    The first change, changing line 13 to "public Sub(){this(5);}", will allow Sub to compile because it calls the constructor of Sub class with an argument of 5.

    The second change, changing line 13 to "public Sub(){super(5);}", will also allow Sub to compile because it calls the constructor of the Super class with an argument of 5.

    Both of these changes provide a valid way to initialize the Sub class without any compilation errors.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 03, 2013
    Quiz Created by
    LanFang
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.