• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please help me

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic