SCJP Questions

Started by certforumz, May 16, 2012, 08:24:56 PM

Previous topic - Next topic

certforumz

Questions
________________________________________
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

________________________________________

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

________________________________________
Question 3)
Which of the following are Java modifiers?

1) public
2) private
3) friendly
4) transient
5) vagrant

________________________________________
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

________________________________________
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

________________________________________
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.

________________________________________
Question 7)
What will happen when you attempt to compile and run this code?
public class Mod{
public static void main(String argv[]){
}
public static native void amethod();
}
1) Error at compilation: native method cannot be static
2) Error at compilation native method must return value
3) Compilation but error at run time unless you have made code containing native amethod available
4) Compilation and execution without error

What will happen when you attempt to compile and run this code?
private class Base{}
public class Vis{
transient int iVal;
public static void main(String elephant[]){
}
}
1)Compile time error: Base cannot be private
2)Compile time error indicating that an integer cannot be transient
3)Compile time error transient not a data type
4)Compile time error malformed main method

________________________________________
Question 9)
What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println(?What a fancy method?);
}
}
//File P2.java
public class P2 extends P1{
afancymethod();
}
1) Both compile and P2 outputs ?What a fancy method? when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile time

________________________________________
Question 10)
You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it.?
public class MyAr{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
1) An error at compile time
2) An error at run time
3) The value 0 will be output
4) The string ?null? will be output

________________________________________
Question 11)
You want to loop through an array and stop when you come to the last element. Being a good java programmer and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use?

1)myarray.length();
2)myarray.length;
3)myarray.size
4)myarray.size();

________________________________________
Question 12)
What best describes the appearance of an application with the following code?
import java.awt.*;

public class FlowAp extends Frame{

public static void main(String argv[]){
FlowAp fa=new FlowAp();
fa.setSize(400,300);
fa.setVisible(true);
}
FlowAp(){
add(new Button(?One?));
add(new Button(?Two?));
add(new Button(?Three?));
add(new Button(?Four?));
}//End of constructor

}//End of Application
1) A Frame with buttons marked One to Four placed on each edge.
2) A Frame with buutons marked One to four running from the top to bottom
3) A Frame with one large button marked Four in the Centre
4) An Error at run time indicating you have not set a LayoutManager

--------------------------------->>Answers------------------------->>
Answer 1)
Objective 1.2)
1) The code will compile and run, printing out the words ?My Func?
A class that contains an abstract method must be declared abstract itself, but may contain non abstract methods.
________________________________________
Answer 2)
Objective 4.1)
4) The code will compile but will complain at run time that main is not correctly defined
In this example the parameter is a string not a string array as needed for the correct main method
________________________________________
Answer 3)
Objective 4.3)
1) public
2) private
4) transient
The keyword transient is easy to forget as is not frequently used. Although a method may be considered to be friendly like in C++ it is not a Java keyword.
________________________________________
Answer 4)
Objective 1.2)
2) The compiler will complain that the Base class is not declared as abstract.
If a class contains abstract methods it must itself be declared as abstract
________________________________________
Answer 5)
Objective 1.2)
1) To get to access hardware that Java does not know about
3) To write optimised code for performance in a language such as C/C++
________________________________________
Answer 6)
Objective 1.2)
4) Success in compilation and output of ?amethod? at run time.
A final method cannot be ovverriden in a sub class, but apart from that it does not cause any other restrictions.
________________________________________
Answer 7)
Objective 1.2)
4) Compilation and execution without error
It would cause a run time error if you had a call to amethod though.
________________________________________
Answer 8)
Objective 1.2)
1)Compile time error: Base cannot be private
A top leve (non nested) class cannot be private.
________________________________________
Answer 9)
Objective 1.2)
4) P1 compiles cleanly but P2 has an error at compile time
The package statement in P1.java is the equivalent of placing the file in a different directory to the file P2.java and thus when the compiler tries to compile P2 an error occurs indicating that superclass P1 cannot be found.
________________________________________
Answer 10)
Objective 1.1)
2) An error at run time
This code will compile, but at run-time you will get an ArrayIndexOutOfBounds exception. This becuase counting in Java starts from 0 and so the 5th element of this array would be i[4].
Remember that arrays will always be initialized to default values wherever they are created.
________________________________________
Answer 11)
Objective 1.1)
2)myarray.length;
The String class has a length() method to return the number of characters. I have sometimes become confused between the two.
________________________________________
Answer 12)
Objective 8.2)
3) A Frame with one large button marked Four in the Centre
The default layout manager for a Frame is the BorderLayout manager. This Layout manager defaults to placing components in the centre if no constraint is passed with the call to the add method.
________________________________________
Answer 13)
Objective 8.2)