It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please help me" Watch "please help me" New topic
Author

please help me

bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
hi guys
please explain how the given answer is correct
Question 5.
Given the following classes, defined in same file named SubClass.java
class BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am BaseClass");
}
}

public class SubClass extends BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am SubClass");
}

public static void main(String [] arg){
BaseClass bc = new SubClass();
bc.sayHello();
}
}
What happens when we compile and run SubClass.java?

A.Does't compile as you cannot override static methods.
B.Compiles but fails at runtime.
C.Compiles and runs successfully with output :
Hi Pal!!!, I am BaseClass
D.Compiles and runs successfully with output :
Hi Pal!!!, I am SubClass
ans c
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
ques 2
Question 8.
Given the code below, what will be the output
class BaseClass{

public void aMethod(){
System.out.println("I am a BaseClass method");
}
}

public class SubClass extends BaseClass{
public void aMethod(){
System.out.println("I am a SubClass method");
super.aMethod();
}

public static void main(String [] arg){

SubClass sc = new SubClass();
sc.aMethod();
}
}

A.Does't Compile as 'super' should be the first statement
to be called inside a method, if at all called.

B.Compiles & fails at runtime.
C.Compiles & runs with output:
I am a SubClass method
I am a BaseClass method
D.Compiles & runs with output:
I am a BaseClass method
I am a SubClass method
ans c
my answer is a
please explain
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
question 3
Question 21.
Given the Code below is in file SubClass.java, What is the result of compiling and running SubClass.java

class BaseClass{

public BaseClass(String s){
System.out.println(s);
}

public BaseClass(int i){
this("I am BaseClass Integer");
}
}

public class SubClass extends BaseClass{

public SubClass(int i){
System.out.println("I am SubClass Integer");
}

public static void main(String[] arg){
SubClass sc = new SubClass();
}
}

A.Does't Compile
B.Compiles but generates runtime error
C.Compiles & runs successfully and the output is :
I am SubClass Integer
I am BaseClass Integer
D.Compiles & runs successfully and the output is :
I am BaseClass Integer
I am SubClass Integer
ans a
how the answer a is correct
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
Question 25.
Given the following class definition in a file named MyClass.java,

import java.io.IOException;

public class MyClass{
int i;
public void amethod(){

try{
i = 10;
i++;
throw new IOException();
i--;
}
catch( IOException e){
i++;
}
finally{
i--;
System.out.println("i is : "+i);
}
} //end of amethod()

public static void main(String[] args){
MyClass mc = new MyClass();
mc.amethod();
} //end of main
} //end of class MyClass
What happens when we try to compile and run MyClass.java.
A.Code does't compile because of error Statement not
reached.
B.Code doesn't compiles because IOException cann't be
thrown explicitly
C.Code compiles & runs with output
i is : 11
D.Code compiles & runs with no output.
ans a
my answer is b how answer a correct
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48

How many String objects are created when we run the following code.
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
ans 3
srikrish
Ranch Hand

Joined: Sep 12, 2000
Posts: 63
ok, this is going to be big reply.....
Q1:When you call static methods with object references (bc.sayHello()), it goes by the type of the variable and not the type of the actual object itself. In other words, bc.sayHello() is equivalent to BaseClass.sayHello(). Since, bc is of type BaseClass, the sayHello method of BaseClass was executed. If the sayHello method was not static, then the SubClass's sayHello method would have been executed because of polymorphism.
Q2: The given answer is right. I think you are confused with super() of constructor and super.normalmethod(). The restriction that you talk about (being the first statement) applies only to a super() call to the CONSTRUCTOR of a base class. Here, aMethod() is not a constuctor. It is a normal method. And the super.aMthod() statement can be anywhere in the aMethod() of subClass.
Q3: When you declare explicit constructors for a class, then you need to define the default constructor too explicitly. Here, the BaseClass does not have a default constructor. So, when the SubClass object is created (and since there is no explicit calls to any of the BaseClass's declared constructors), it will place a call to BaseClass() (default) constructor of the BaseClass. Since it doesn't find it, it will give a compile error.
Q4: Once a method throws an Exception, the control gets out of the method. So, the i-- statement is never executed here. And the compiler objects to such unreachable code.
Also, IOException can be thrown since it implements the Throwable interface.
Q5: These are the objects that are created by this code:
1. "Hello"
2. "Pal"
3. "HelloPal" (coz of s2+"Pal")
s1 and s2 both point to the "Hello" object. s3 and s4 both point to the "HelloPal" object.
Hope the explanations are clear.
Srikrish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help me
 
Similar Threads
Queries
Constructors.
Overriding static methods - pl explain!
Constructor question
Doubt in overrididng