Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Some Runtime vs. Compile Time SCJP 6.0 Mock Exam Questions

 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sashank Sharma asked about recognizing exceptions at runtime vs. errors at compile time vs. RuntimeExceptions on this JavaRanch thread.

I thought I'd just contribute some of the mock SCJP6 certification exam questions I'm giving some of my students that hits upon the subject. If they help and contribute to the subject, that's great. If you can find any errors or typos, feel free to let me know. You can comment on my blog if you like.

The answers are below.

1. What is the result of attempting to compile and run the following public class?

public class ErrorTester {

public static void main(string args[]) throws Throwable{
System.out.println("Hello World");
}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but with no output
d) The class will compile and run with the expected output



####################################################

2. What is the result of attempting to compile and run the following public class?

public class ErrorTester{
static public void main(String args[]) throws Throwable {
System.out.println("Hello World");
throw new NoSuchMethodException();
}
}

a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print out Hello World
d) The class will compile and run and print out Hello World and throw an Exception



#########################################################

3. What is the result of attempting to compile and run the following public class?

public class ErrorTester{

public static void main(String args[]) throws Throwable {
throw new NoSuchMethodException();
System.out.println("Hello World");
}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print out Hello World
d) The class will compile and run and print out Hello World and throw an Exception



#########################################

4. What is the result of attempting to compile and run the following public class?

public class ErrorTester{
public void static main(String args[]) throws Throwable {
System.out.println("Hello World");
}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print out Hello World
d) The class will compile and run and print out Hello World and throw an Exception



####################################################



5. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x;
for (int i = 0; i<10; i++) {
if (i<5) x = i;
else x=0;
System.out.print(x);
}
}
}




a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print 0123400000
d) The class will compile and run and print 1234567890
e) The class will compile and run and print 012340000
f) The class will compile and run and print 123456789




###############################################

6. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x;
for (int i = 0; i<10; i++) {
if (i<5) x = i;
else i=10;
System.out.print(x);
}
}
}




a) The class will not compile.
b) The class will compile but not run
d) The class will compile and run and print 01234
e) The class will compile and run and print 12345
f) The class will compile and run and print 4


################################################################################


7. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x;
for (int i = 0; i<10; i++) {
if (i<5) x = i;
else {i=10; x = 0;}
System.out.print(x);
}
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print 123450
d) The class will compile and run and print 012340
e) The class will compile and run and print 5
f) The class will compile and run and print 0



########################################################

8. What is the result of attempting to compile and run the following class?

public class ErrorTester{
public static void main(String args[]) throws Throwable {
String input = (new java.util.Scanner( System.in )).nextLine();
double timesTwo = 2 x new Double(input);
System.out.println(input);
}
}

a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but print out nothing
d) The class will compile and run and throw an Exception
e) The class will compile and run and might throw a RuntimeException


###################################################################


9. What is the result of attempting to compile and run the following class?

public class ErrorTester{
public static void main(String args[]) throws Throwable {
String input = (new java.util.Scanner( System.in )).nextLine();
double timesTwo = 2 * new Double(input);
System.out.println(input);
}
}

a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but print out nothing
d) The class will compile and run and throw an Exception
e) The class will compile and run and might throw a RuntimeException



#####################################################################

10. What is the result of attempting to compile and run the following class with the input of 123?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
String input = (new java.util.Scanner( System.in )).nextLine();
double timesTwo = 2 * new Double(input);
System.out.println(input);
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but print out nothing
d) The class will compile and run but print out 123
e) The class will compile and run but print out 246



##################################################################################


11. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x = 10;
int y = 5;
int z = (long)x*y;
System.out.println(z);
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but throw a casting Exception at runtime
d) The class will compile and run but print 50


#####################################################

12. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x = 10;
char y = 5;
long z = x*y;
System.out.println(z);
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but throw a casting Exception at runtime
d) The class will compile and run but print 50





****************************


1. What is the result of attempting to compile and run the following public class?

public class ErrorTester throws Throwable{

public static void main(string args[]) { System.out.println("Hello World");}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but with no output
d) The class will compile and run with the expected output


Option a) is correct.

The main method spells the word String incorrectly, and as a result, the code simply will not compile. This class will not even make it to the running stage, as no compiled, ErrorTester.class file will be generated, and there would be no class file to run. If the main method spelled string as String, the code would compile and output Hello World to the console.


####################################################

2. What is the result of attempting to compile and run the following public class?

public class ErrorTester{
static public void main(String args[]) throws Throwable {
System.out.println("Hello World");
throw new NoSuchMethodException();
}
}

a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print out Hello World
d) The class will compile and run and print out Hello World and throw an Exception

In this case, option d) is correct. The code will indeed compile, as the syntax is solid. The main method is properly structured, so the class will indeed run. Notice that the words static and public are transposed? That's not typical, but it is legal, as the keywords that decorate the method, before defining the return type, need not go in any particular order.

The class will indeed run, and the text Hello World will indeed go to the console. But after the text is printed out, an Exception will be thrown, the program will terminate, and administrators will be going through the logs and trying to figure out why your application keeps printing out:

Hello World
Exception in thread "main" java.lang.NoSuchMethodException
at ErrorTester.main(ErrorTester.java:4)

#########################################################

3. What is the result of attempting to compile and run the following public class?
public class ErrorTester{

public static void main(String args[]) throws Throwable {
throw new NoSuchMethodException();
System.out.println("Hello World");
}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print out Hello World
d) The class will compile and run and print out Hello World and throw an Exception

Option a) is correct. This code will cause a compile error.

In this example, the compiler recognizes that the programmer codes an instruction after an exception is positively thrown. Since the exception is thrown, not code following the exception will be executed if there is no try-catch block in existence. As a result, the compiler coughs up, indicating:

C:\_jdk1.6\bin>javac ErrorTester.java
ErrorTester.java:4: unreachable statement
System.out.println("Hello World");
^
1 error

#########################################

4. What is the result of attempting to compile and run the following public class?

public class ErrorTester{
public void static main(String args[]) throws Throwable {
System.out.println("Hello World");
}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print out Hello World
d) The class will compile and run and print out Hello World and throw an Exception

Option a) is correct.

This code places a method decorator after the return type of the main method. Placing anything before the method name other than a return type is not allowed by the compiler, so this code would not make it past the compiler, and no Java class file would ever be generated.

Note that placing a decorator after the return type is not just invalid for the main method. This particular questions completely messes up the runnable main method, but making such a mistake for any method, not just the runnable main, would generate a compile error.

####################################################



5. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x;
for (int i = 0; i<10; i++) {
if (i<5) x = i;
else x=0;
System.out.print(x);
}
}
}




a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print 0123400000
d) The class will compile and run and print 1234567890
e) The class will compile and run and print 012340000
f) The class will compile and run and print 123456789

In this case, option c) is correct.

There are not trick in this question, other than understanding application flow and Java syntax. Leaving the braces, {}, off the if and else block is nasty, and it is a very bad programming practice, but it is valid. When braces are missing after an if or else keyword, the first line after the if or else is considered to be the complete body of the code block, making this code completely valid.

When run, the output is: 0123400000


###############################################

6. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x;
for (int i = 0; i<10; i++) {
if (i<5) x = i;
else i=10;
System.out.print(x);
}
}
}




a) The class will not compile.
b) The class will compile but not run
d) The class will compile and run and print 01234
e) The class will compile and run and print 12345
f) The class will compile and run and print 4

Option a) is correct.

In this case, the compiler does not know whether x ever gets initialized. All variables must be initialized before being used, and since the variable x is initialized in the if block, the compiler worries that the if block is never executed, the variable x will never be initialized, and subsequently, the System.out.println(x); statement will be printing out an uninitialized variable, which is not allowed.

################################################################################


7. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x;
for (int i = 0; i<10; i++) {
if (i<5) x = i;
else {i=10; x = 0;}
System.out.print(x);
}
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run and print 123450
d) The class will compile and run and print 012340
e) The class will compile and run and print 5
f) The class will compile and run and print 0

In this case, option d) is correct.

Although the missing brackets around the code in the if block is unconventional, it is valid, so long as there is only one statement that makes up the body of the block. Since the code is valid and compiles, this questions just gets down to following the execution of the method, which results in the value 012340 being printed out to the console.

########################################################

8. What is the result of attempting to compile and run the following class?

public class ErrorTester{
public static void main(String args[]) throws Throwable {
String input = (new java.util.Scanner( System.in )).nextLine();
double timesTwo = 2 x new Double(input);
System.out.println(input);
}
}

a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but print out nothing
d) The class will compile and run and throw an Exception
e) The class will compile and run and might throw a RuntimeException

In this case, option a) is correct.

The program clearly tries to multiply two number together, but the operator for multiplication is a star, *, not an x. As a result, this code will not compile, and not get past the compilation stage.

###################################################################


9. What is the result of attempting to compile and run the following class?

public class ErrorTester{
public static void main(String args[]) throws Throwable {
String input = (new java.util.Scanner( System.in )).nextLine();
double timesTwo = 2 * new Double(input);
System.out.println(input);
}
}

a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but print out nothing
d) The class will compile and run and throw an Exception
e) The class will compile and run and might throw a RuntimeException

Option e) is correct.

This program takes input from the user, and multiplies that input by two. However, if the input is not numeric, the creation of the Double, new Double(input), will fail, triggering the following runtime Exception message:

C:\_jdk1.6\bin>java ErrorTester
a
Exception in thread "main" java.lang.NumberFormatException: For input string: "a
"at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:12
24)at java.lang.Double.valueOf(Double.java:475)at java.lang.Double.(Double.java:567)at ErrorTester.main(ErrorTester.java:6)


#####################################################################

10. What is the result of attempting to compile and run the following class with the input of 123?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
String input = (new java.util.Scanner( System.in )).nextLine();
double timesTwo = 2 * new Double(input);
System.out.println(input);
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but print out nothing
d) The class will compile and run but print out 123
e) The class will compile and run but print out 246

In this case, option d) is correct.

While is appears that the program wants to print out twice the value entered by the user, the fact is, the original input, with the variable name input, is printed to the console. As such, if the user entered 123 into the console, this program would print 123 back out to the console, making option d) correct.

##################################################################################


11. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x = 10;
int y = 5;
int z = (long)x*y;
System.out.println(z);
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but throw a casting Exception at runtime
d) The class will compile and run but print 50

Option a) is correct.

The compiler recgonizes that the return of x*y is an int, and it is being cast into a long, so the compiler does not allow this, generating the following error message:

C:\_jdk1.6\bin>javac ErrorTester.java
ErrorTester.java:6: possible loss of precision
found : long
required: int
int z = (long)x*y;
^
1 error



#####################################################

12. What is the result of attempting to compile and run the following class?


public class ErrorTester{
public static void main(String args[]) throws Throwable {
int x = 10;
char y = 5;
long z = x*y;
System.out.println(z);
}
}



a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but throw a casting Exception at runtime
d) The class will compile and run but print 50

Option d) is correct.

In this case, the code is completely valid, and the class will both compile and run, generating an output of 50.

A common misconception is that a char is purely a character type. In fact, the char can take on numeric values just as easily as it can take on character data. In this case, the char holds the value of 5, and when multiplied by 10, a value of 50 is returned and subsequently printed to the console.


#########################################
[ July 19, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron, have you now set a new record for the longest thread at JR ?

Thanks for sharing your work
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Cameron,

thanks for the questions. It's a good training solving them.

There is an error in question 5:

I think it should be i>5, because the actual result is 0000006789.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cameron,

good work. I like the detailed explanations. Two remarks:

1. The program in exercise 1 wouldn't compile even with correct main method, because of "class ErrorTester throws Throwable".

2. In my opinion the output in exercise 5 is 0123400000.
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simon and Ralph, you are clearly in weekend mode, because if you paid proper attention to the original post, you would see that the throws IS on the main method, and the output you suggest IS marked as the correct answer.

Please pay proper attention!

And just so you know, I did not, in any way, go over and edit the original post to correct any mistakes to make you guys look bad. I'm a man of integrity.

Thanks for your feedback. I really didn't expect anyone to go over them that thoroughly, let alone an hour after the original posting.

I updated it on my SCJP6 Mock Exam Questions blog, and added a new question where the throws clause was on the class declaration. That's actually a good little stumper if someone (like me ) isn't paying proper attention.


13. What is the result of attempting to compile and run the following public class?

public class ErrorTester throws Throwable{

public static void main(String args[]) { System.out.println("Hello World");}
}


a) The class will not compile.
b) The class will compile but not run
c) The class will compile and run but with no output
d) The class will compile and run with the expected output


Option a) is correct.

The problem with this code is the fact that the throws clause is on the class, not the main method. Only methods can declare that they throw an exception, not a class. As a result, this method does not compile.

This is a pretty blatant coding error, but sometimes, when you really get deep into the SCJP questions, an obvious coding error can be difficult to spot, so be careful!



Kindest regards!

-Cameron McKenzie
[ July 19, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The source code for question 4 is messed up:



One too many declarations of the ErrorTester class (due to a copy/paste error?).
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jelle Klap:
The source code for question 4 is messed up:



It is NOT ...Lies! Utter LIES!

Thanks for the catch.



-Cameron McKenzie
[ July 19, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron,

you fooled me. No wonder why SCJP is such a horrible exam, if I think of the persons who write questions ...

But in return, now you've got 13 questions and 13 brings bad luck. Watch out, terrible, mysterious things will happen to you.
[ July 20, 2008: Message edited by: Ralph Jaus ]
 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice mock! Give different ideas on questions..

Thank you
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Nooooooo, I got 5 wrong answers.

The 2 x new Double was a typing mistake I thought, same is the case with string instead of String. I missed the void static mistake, and the 6th and 9th questions were complete mind boggling ones.

But still, it was a great experience...

Thanks Cameron for this
[ October 12, 2008: Message edited by: Ankit Garg ]
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explanation of question 11 is incorrect.



The explicit cast to long applies to x not to x*y. Casting (converting) x to long is permitted. (Converting of x*y to long would also be legal.)
The error occurs because the result of (long)x*y is long and it is being assigned to int variable which is not allowed.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice questions I got all correct

10. What is the result of attempting to compile and run the following class with the input of 123?



Question no. 10 should be as follows

10. What is the result of attempting to compile and run the following class with the input of 123 ?

there should be space between 123 and ?

if there is no space beteween 123 and ? then output would be
The class will compile and run and throw a RuntimeException

Thanks Cameron for sharing with us its very nice

This is the place where we can communicate with our great authors

Regards
Ninad
 
I am displeased. You are no longer allowed to read this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic