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{a) The class will not compile.
static public void main(String args[]) throws Throwable {
System.out.println("Hello World");
throw new NoSuchMethodException();
}
}
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
d) The class will compile and run and print 0000056789
e) The class will compile and run and print 0000006789
f) The class will compile and run and print 00000678910
###############################################
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
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
#########################################
#########################################
***************************************************
#########################################
#########################################
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{a) The class will not compile.
static public void main(String args[]) throws Throwable {
System.out.println("Hello World");
throw new NoSuchMethodException();
}
}
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.
#####################################################################
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.
#####################################
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, and generates the following, very clear and precise, error message:
C:\_jdk1.6\bin>javac ErrorTester.java
ErrorTester.java:1: '{' expected
public class ErrorTester throws Throwable{
^
1 error
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!
No comments:
Post a Comment