aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Section 2 mock test questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Section 2 mock test questions" Watch "Section 2 mock test questions" New topic
Author

Section 2 mock test questions

Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
Here are my made up questions for section 2. Please comment or point out any poorly worded questions.

1 What will the following code output?



1 Compilation fails
2 An exception occurs at runtime
3 12false
4 true
5 12true
6 5true


2 Which two of the following lines will fail to compile?

1) int x = 1; switch (x) {}
2) double d = 2; switch(d) {}
3) int [] a = {1,2,3}; switch (a.length) {}
4) char c = 'b'; switch (c) {}
5) Integer i = new Integer(2); switch (i) {}
6) int z = 2; switch (z*2) {}

3 What will be the output of the following code?



1 Compilation fails
2 An exception occurs at runtime
3 11
4 00
5 1345678910
6 135791111
7 134567899

4 What will be the output of the following code?



1 Compilation fails
2 An exception occurs at runtime
3 0123
4 00
5 01230123
6 0123012301230
7 012301230123

5 Which two of the following statements are false?

1 All exception classes derive from Throwable
2 You can catch multiple types of exceptions with a single catch clause
3 The finally block is ALWAYS executed
4 You need to explicitly handle RuntimeException for code to compile
5 You can throw an exception out of main instead of handling it
6 It is legal to use a try without a catch if you have a finally clause
7 The method printStackTrace() prints the most recent method first

6 If you wish to throw an exception of type MyExcept, what is the proper line to replace // Here



1 throw new MyExcept();
2 throws MyExcept;
3 throw MyExcept();
4 MyExcept.throw;
5 new MyExcept();
6 throw MyExcept;

7 If you compile and run the following program, what will its output be?



1 Compile time error
2 abException in thread "main" java.lang.ArithmeticException
3 abcInfinity
4 abcd10
5 abe10
6 abd10
7 abe0

8 Consider the following code. What will its output be?



1 Compile time error
2 14
3 144
4 124
5 1404
6 24


9 Which three of the following lines could be legal and appropriate uses of assert?

1 assert count > 0: count++;
2 assert students < 30: students;
3 assert teachers < 20: System.out.print(teachers);
4 assert score > 0;
5 assert true;
6 assert false;


10 Which output is the closest to the real output of the following program if compiled with assertions and run with assertions enabled?



1 Compile error
2 zero Exception in thread "main" java.lang.AssertionError: zero
3 bigger Exception in thread "main" java.lang.AssertionError: bigger
4 zero
5 bigger smaller
6 zero zero Exception in thread "main" java.lang.AssertionError
7 bigger bigger Exception in thread "main" java.lang.AssertionError



11 Which two of the following statements are true?

1 It is often appropriate to use assert false on default cases of switch statements
2 You should use assertions to check values passed in to public methods
3 You should use assert to validate code that should never be reached is not reached
4 Assertions should be caught with a try catch block
5 assert true will throw an AssertionError
6 Causing side affects with assertions is correct for debugging


12 Which two of the following statements are false?

1 You can use the command line java -ea to enable assertions
2 You can use assert as an identifier and have your code compile without error
3 Assertions are typically on during debugging and off in the production version
4 You can use the command line java -source 1.4 to enable assertions
5 Assertions are a replacement for exceptions
6 The word assert is a keyword as of version 1.4

1- Objective 2.1

6 5 true

It is valid to have any expression which evaluates to a boolean as the argument for an if statement. Both the a = 4 and b = true are assignments, not comparisons. Thus b = true evaluates to true, and a is incremented to 5. The System.out.print(a) always executes regardless of the if statement above it.

2- Objective 2.1

2,5

2- can not switch on any type except those which can be promoted to integer
5- Integer is an Object, not a primitive which can be switched on

3- Objective 2.2

5- 1345678910

4- Objective 2.2

1 Compilation fails

The continue should read "continue outside;" and the break "break inside;". If it were corrected in this way, the output would be 0123012301230

5 Objective 2.3

3,4 are false

1 - True, all exception classes also derive from Object
2 - You catch multiple types of exceptions which all derive from your caught
exception
3 - False, a System.exit(0); in the catch will skip the finally from executing
4 - False, you do not need to explicitly handle RunTimeExceptions
5 - True, you can throw an exception out of main
6 - This is true, try writing the code

6 Objective 2.3

1 throw new MyExcept(); is correct


7 Objective 2.4

6 - abd10

The division by zero on an integer does throw an AirthmeticException which is caught by the first catch clause.

8 Objective 2.3

1 - Compile time error

A compile time error is generated by the line "System.out.print("2")"

9 Objective 2.5

2,4,6

1 False it is not appropriate to modify a value as a side effect of an assert
2 True- this is an appropriate check and will print out the number of students
3 False System.out.print returns a type void, which is not allowed
4 True this will throw an AssertionError if score is not greater than 0
5 False assert true will never do anything
6 True assert false is appropriate in code you think will never execute

10 Objective 2.5

3 bigger Exception in thread "main" java.lang.AssertionError: bigger

This first assert comes out to be false. It prints out the string, then returns this which is appended to the Exception error

11 Objective 2.6

1,3 are true

1 True- Often switch statements should not use default, and assert false is a good way to discover if they are hitting the default handler by mistake
2 False- assertions should not be to check your publically exposed interfaces, this should be done with if statements
3 True- this is a good way to use the assert mechanism
4 False- assertions should not be handled- they should terminate the program
5 False- assert false will throw an AssertionError
6 False- assertions should not cause side effects

12 Objective 2.6

4,5 are false

1 True you use java -ea to enable assertions
2 True- code compiled not as source 1.4 can use assert but it will generate warnings
3 True- assertions are typically used for the testing environment only
4 False, you use the command line javac - source 1.4 to compile in assertions
5 False, assertions are not a replacement for exceptions
6 True- assert is a keyword as of version 1.4



Section 2: Flow control, Assertions, and Exception Handling

1 Write code using if and switch statements and identify legal argument types for these statements.

2 Write code using all forms of loops including labeled and unlabeled, use of break and continue, and state the values taken by loop counter variables during and after loop execution.

3 Write code that makes proper use of exceptions and exception handling clauses (try, catch, finally) and declares methods and overriding methods that throw exceptions.

4 Recognize the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination).

5 Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions.

6 Identify correct statements about the assertion mechanism.
[ September 16, 2004: Message edited by: Tom Tolman ]
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Tom,

Just curious.

Are you trying to given Dan C. "a run for his money" on mock exam questions?

To understand Java better yourself??

Why?

jc


Author and Instructor, my book
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
I have a few goals:

1) To come up with at least one, possibly two mock exams which can help people study (I have really appreciated all of the mock exams others have done)

2) To help me further understand the details of the questions and of the Java language. Already I have discovered a number of interesting things making up these questions.

3) To help me understand the mindset of the exam makers.

I hope you try the exam questions. I am trying to model the actual questions as closely as possible (you are always told the number of correct answers, and the incorrect answers shown reflect common misperceptions (I hope).
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
Hi Tom ,

Please have a look at your Question 7. The output you said is right ONLY IF, you have an additional "}" at the end. That was missing in your code, which made me select Answer:1 (Compilation Fails)

The efforts you put here are really great and your questions are really nice. Thanks for that


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
 
Consider Paul's rocket mass heater.
 
subject: Section 2 mock test questions