SCJP Mock Exam 3
question 1)
what will happen when you attempt to compile and run this code?
abstract class base{
abstract public void myfunc();
public void another(){
system.out.println('another method');
}
}
public class abs extends base{
public static void main(string argv[]){
abs a = new abs();
a.amethod();
}
public void myfunc(){
system.out.println('my func');
}
public void amethod(){
myfunc();
}
}
1) the code will compile and run, printing out the words 'my func'
2) the compiler will complain that the base class has non abstract methods
3) the code will compile but complain at run time that the base class has non abstract methods
4) the compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it
answer to question 1
--------------------------------------------------------------------------------
question 2)
what will happen when you attempt to compile and run this code?
public class mymain{
public static void main(string argv){
system.out.println('hello cruel world');
}
}
1) the compiler will complain that main is a reserved word and cannot be used for a class
2) the code will compile and when run will print out 'hello cruel world'
3) the code will compile but will complain at run time that no constructor is defined
4) the code will compile but will complain at run time that main is not correctly defined
answer to question 2
--------------------------------------------------------------------------------
question 3)
which of the following are java modifiers?
1) public
2) private
3) friendly
4) transient
5) vagrant
answer to question 3
--------------------------------------------------------------------------------
question 4)
what will happen when you attempt to compile and run this code?
class base{
abstract public void myfunc();
public void another(){
system.out.println('another method');
}
}
public class abs extends base{
public static void main(string argv[]){
abs a = new abs();
a.amethod();
}
public void myfunc(){
system.out.println('my func');
}
public void amethod(){
myfunc();
}
}
1) the code will compile and run, printing out the words 'my func'
2) the compiler will complain that the base class is not declared as abstract.
3) the code will compile but complain at run time that the base class has non abstract methods
4) the compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it
answer to question 4
--------------------------------------------------------------------------------
question 5)
why might you define a method as native?
1) to get to access hardware that java does not know about
2) to define a new data type such as an unsigned integer
3) to write optimised code for performance in a language such as c/c++
4) to overcome the limitation of the private scope of a method
answer to question 5
--------------------------------------------------------------------------------
question 6)
what will happen when you attempt to compile and run this code?
class base{
public final void amethod(){
system.out.println('amethod');
}
}
public class fin extends base{
public static void main(string argv[]){
base b = new base();
b.amethod();
}
}
1) compile time error indicating that a class with any final methods must be declared final itself
2) compile time error indicating that you cannot inherit from a class with final methods
3) run time error indicating that base is not defined as final
4) success in compilation and output of 'amethod' at run time.
answer to question 6
--------------------------------------------------------------------------------
question 7)
what will happen when you attempt to compile and run this code?