Right Place For Right PersonTM 
Sponsored Ads
Home Tutorials Articles Forums Source Code Books Certifications Interviews Questions

Previous Home Next

Sun Java Certification Program

Object Oriented programming

Questions 1 Assume we have the following code in the file /abc/def/Q.java:
//File: /abc/def/Q.java:
package def;

public class Q {
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
}

And this code is in /abc/Tester.java:
//File: /abc/Tester.java:
import def.Q;

public class Tester extends Q {
Q sup = new Q();
Sub sub = new Sub();

public void someMethod() {
// First, try to refer to sup's memebers.
sup.privateVar = 1; // Line 1
sup.packageVar = 2; // Line 2
sup.protectedVar = 3; // Line 3
sup.publicVar = 4; // Line 4

// Next, try to refer to this object's members
// supplied by class Q.
privateVar = 5; // Line 5
packageVar = 6; // Line 6
protectedVar = 7; // Line 7
publicVar = 8; // Line 8

// Next, let's try to access the members of
// another instance of Tester.
Tester t = new Tester();
t.privateVar = 9; // Line 9
t.packageVar = 10; // Line 10
t.protectedVar = 11; // Line 11
t.publicVar = 12; // Line 12

// Finally, try to refer to the members in a
// subclass of Tester.
sub.privateVar = 13; // Line 13
sub.packageVar = 14; // Line 14
sub.protectedVar = 15; // Line 15
sub.publicVar = 16; // Line 16
}
}

And this code is in /abc/Sub.java:
//File: /abc/Sub.java:
public class Sub extends Tester {
}
Assume the directory, /abc, is in the compiler's CLASSPATH.
When you try to compile Tester.java there will be several compiler errors. For each of the labeled lines above, decide whether or not a compiler error will be generated.

Questions 2 Given the following listing of the Widget class:
class Widget extends Thingee { //(1)
static private int widgetCount = 0; //(2)
public String wName; //(3)
int wNumber; //(4)
static synchronized int addWidget() { //(5)
wName = “I am Widget # ” + widgetCount; //(6)
return widgetCount; //(7)
} //(8)
public Widget() { //(9)
wNumber = addWidget(); //(10)
} //(11)
} //(12)
What happens when you try to compile the class and use multiple Widget objects in a program?
(a) The class compiles and each Widget will get a unique wNumber and wName reflecting the order in which the Widgets were created.
(b) The compiler objects to line 6.
(c) The class compiles, but a runtime error related to the access of the variable wName occurs in the addWidget method.

Questions 3 The following method definition is designed to parse and return an integer from an input string that is expected to look like “nnn,ParamName.” In the event of a NumberFormatException, the method is to return –1.
public int getNum (String s) { //(1)
try { //(2)
String tmp = S.substring(0, S.indexOf(‘,’)); //(3)
return Integer.parseInt(tmp); //(4)
} catch (NumberFormatException e) { //(5)
System.out.println(“Problem in ” + tmp); //(6)
} //(7)
return –1; //(8)
} //(9)
What happens when you try to compile this code and execute the method with an input string that does contain a comma separating the number from the text data?
(a) A compiler error in line 6 prevents compilation.
(b) The method prints the error message to standard output and returns –1
(c) A NullPointerException is thrown in line 3.
(d) A StringIndexOutOfBoundsException is thrown in line 3.

Questions 4 Your chief Software designer has shown you a sketch of the new Computer parts system she is about to create. At the top of the hierarchy is a Class called Computer and under this are two child classes. One is called LinuxPC and one is called WindowsPC.
The main difference between the two is that one runs the Linux operating System and the other runs the Windows System (of course another difference is that one needs constant re-booting and the other runs reliably). Under the WindowsPC are two Sub classes one called Server and one Called Workstation. How might you appraise your designers work?
(a) Give the goahead for further design using the current scheme.
(b) Ask for a re-design of the hierarchy with changing the Operating System to a field rather than Class type.
(c) Ask for the option of WindowsPC to be removed as it will soon be obsolete.
(d) Change the hierarchy to remove the need for the superfluous Computer Class.

Questions 5 Given the following class definition which of the following can be legally placed after the comment line //Here ?
class Base{
public Base(int i){}
}
public class MyOver extends Base {
public static void main(String arg[]) {
MyOver m = new MyOver(10);
}
MyOver(int i){
super(i);
}
MyOver(String s, int i){
this(i);
//Here
}
}
(a) MyOver m = new MyOver();
(b) super();
(c) this("Hello",10);
(d) Base b = new Base(10);

Questions 6 Suppose you have two classes defines as follows:
class ApBase extends Object implements Runnable
class ApDerived extends ApBase implements Observer
Given two variables created as follows:
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
Which of the following Java statements will compile and execute without error?
(a) Runnable rn = aDer;
(b) Runnable rn2 = (Runnable) aBase;
(c) Observer ob = aBase;
(d) Observer ob2 = (Observer) aBase;

Questions 7 Suppose we have an ApBase class declared as:
class ApBase extends Object implements Runnable
The following code fragment takes a reference to an ApBase object and assigns it to a variety of variables. What will happen when you try to compile and run this code?
ApBase aBase = new ApBase();
Runnable aR = aBase;
Object obj = aR;
ApBase x = (ApBase)obj;
(a) The compiler objects to line 2
(b) The compiler objects to line 3
(c) The code compiles but when run throws a ClassCastException in line 4
(d) The code compiles and runs without problem.

Questions 8 Suppose you have two classes defines as follows:
class ApBase extends Object implements Runnable
class ApDerived extends ApBase implements Observer
Given two variables created as follows:
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
Which of the following Java statements will compile and execute without error?
(a) Object obj = aBase; Runnable rn = obj;
(b) Object obj = aBase; Runnable rn = (Runnable)obj;
(c) Object obj = aBase; Observer ob = (Observer)aBase;
(d) Object obj = aDer; Observer ob2 = obj;

Questions 9 What will happen if you attempt to compile and run the following code?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}

public class CEx {
public static void main(String argv[]){
Base b = new Base();
Sub s = (Sub) b;
}
}
(a) Compile and run without error.
(b) Compile time Exception.
(c) Runtime Exception.

Questions 10 You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java ?
//Base.java
package Base;
class Base {
protected void amethod() {
System.out.println("amethod");
}
}

//Class1.java
package Class1;
public class Class1 extends Base {
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}
}
(a) Compile Error: Methods in Base not found
(b) Compile Error: Unable to access protected method in base class
(c) Compilation followed by the output "amethod"
(d) Compile error: Superclass Class1.Base of class Class1.Class1 not found

Questions 11 You are taking over an aquarium simulation project. Your predecessor had created a generic Fish class that includes an oxygenConsumption method declared as follows:
public float oxygenConsumption(float temperature)
The aquarium simulation sums oxygen consumption for all fish in the tank with the following code fragment, where fishes is an array of Fish object references:
float total = 0;
for (int i = 0; i < fishes.length; i++) {
total += fishes[i].oxygenConsumption(t);
}
you are writing a subclass for a particular fish species. Your task is to provide a method with species-specific metabolism data that will transparently fit into the simulation. Do you want to overload or override the oxygenConsumption method?
(a) overload.
(b) override.

Questions 12 The GenericFruit class declares the following method to return a float number of calories in the average serving size: [3]
public float aveCalories()
Your Apple class, which extends GenericFruit, overrides this method. In a DietSelection class that extends Object, you want to use the GenericFruit method on an Apple object. Select the correct way to finish the statement in the following code fragment so the GenericFruit version of aveCalories is called using the gf reference, or select option (d)
GenericFruit gf = new Apple();
float cal = // finish this statement using gf
(a) gf.aveCalories();
(b) ((GenericFruit)gf).aveCalories();
(c) gf.super.aveCalories();
(d) There is no way to call the GenericFruit method.

Questions 13 What will be the result of compiling and running the given program?
Select one correct answer.
1 public class Child extends Parent
2 {
3 public static int test(int i)
4 {
5 return 20;
6 }
7 public static void main(String[] args)
8 {
9 Parent c = new Child();
10 System.out.println(c.test(10));
11 }
12 }
13 class Parent
14 {
15 public static int test(int i)
16 {
17 return 5;
18 }
19 }
(a) Compile time as we can't overide static methods.
(b) Run time error as we can't overide static methods.
(c) Program compiles correctly and prints 5 when executed.
(d) Program compiles correctly and prints 20 when executed.

Questions 14 What will be the result of compiling and running the given program?
Select one correct answer.
1 class sample
2 {
3 sample(String s)
4 {
5 System.out.println("String");
6 }
7 sample(Object o)
8 {
9 System.out.println("Object");
10 }
11 }
12 class constructor
13 {
14 public static void main(String arg[])
15 {
16 sample s1=new sample(null);
17 }
18 }
(a) Compile time error as call to constructor at line no. 16 is ambigious.
(b) Run time error as call to constructor at line no. 16 is ambigious.
(c) Program compiles correctly and prints "object" when executed.
(d) Program compiles correctly and prints "string" when executed.

Questions 15 What will be the result of compiling and running the given program?
Select one correct answer.
1 class sample
2 {
3 sample(String s)
4 {
5 System.out.println("String");
6 }
7 sample(StringBuffer sb)
8 {
9 System.out.println("StringBuffer");
10 }
11 }
12 class constructor
13 {
14 public static void main(String arg[])
15 {
16 sample s1=new sample(null);
17 }
18 }
(a) Compile time error as call to constructor at line no. 16 is ambigious.
(b) Run time error as call to constructor at line no. 16 is ambigious.
(c) Program compiles correctly and prints "StringBuffer" when executed.
(d) Program compiles correctly and prints "string" when executed.

Questions 16 What will be the result of compiling and running the given program?
Select one correct answer.
1 class A
2 {
3 void callme()
4 {
5 System.out.println("A");
6 }
7 int r=10;
8 }
9 class B extends A
10 {
11 public void callme()
12 {
13 System.out.println("B");
14 }
15 int r=20;
16 }
17 class Q16
18 {
19 public static void main(String args[])
20 {
21 A a = new B();
22 a.callme();
23 System.out.println(a.r);
24 }
25 }
(a) Compile time error.
(b) Program compiles correctly and prints "A" and 10 when executed.
(c) Program compiles correctly and prints "B" and 20 when executed.
(d) Program compiles correctly and prints "B" and 10 when executed.

Questions 17 Whether the following code compile or not?
Select any two.
1 class Base {}
2 class Agg extends Base
3 {
4 public String getFields()
5 {
6 String name = "Agg";
7 return name;
8 }
9 }
10 public class Inheritence
11 {
12 public static void main(String argv[])
13 {
14 Base a = new Agg();
15 System.out.println(a.getFields());
16 }
17 }
(a) It will compile.
(b) It will not compile.
(c) It will compile if we cast the variable a for the object of class Agg like ((Agg) a).getFields()
(d) We can cast the variable a for the object of class Agg, but it is not required.

Answers: 1 When one class extends another class, instances of the subclass will contain their own copies of ALL members declared in the superclass (including any private variables). In the example above all instances of Tester will contain all four of the variables declared in Q. However, not all of these variables will be accessible (i.e., visible) by instances of Tester.
In the descriptions below, we say a variable is inherited by a subclass if it is accessible to instances of the subclass.
sup.privateVar = 1; // Line 1
private members are only accessible from within the class in which they are declared. Because privateVar was declared in class Q, it is not accessible from class Tester. The following compiler error results:
Tester.java:10: privateVar has private access in def.Q
sup.privateVar = 1; // Line 1
^
sup.packageVar = 2; // Line 2
package members are only accessible to classes within the same package as the class that declares them. In the case, the member packageVar, is declared in class Q, in the def package. package is accessible only from within classes in the def package. Since the Tester class is in the "default package" it does not have access to packageVar. The following compiler error results:
Tester.java:11: packageVar is not public in def.Q; cannot be accessed from outside package
sup.packageVar = 2; // Line 2
^
sup.protectedVar = 3; // Line 3
Even though class Tester extends class Q, instances of Tester are not allowed access to protected members in instances of Q, proper. Because sup is an instance of Q, proper, a Tester instance will not have access to its protected members. An instance of class Tester can only access protected members in instances of classes that are of type Tester. This includes classes that inherit from Tester (such as the Sub class). The following compiler error results:
Tester.java:12: protectedVar has protected access in def.Q
sup.protectedVar = 3; // Line 3
^
sup.publicVar = 4; // Line 4
public members are available "everywhere". Consequently, Tester's reference to sup.publicVar compiles without error.

privateVar = 5; // Line 5
private members are only accessible from within the class in which they are declared. Because privateVar was declared in class Q, it is not accessible from class Tester. Note that on Line 1 we were trying to refer to a private member in another object (the member sup). On line 5 we are trying to refer the copy of privateVar that Tester contains by virtue of being a subclass of Q. In terms of the visibility of privateVar, however, it does not matter. privateVar is a private member declared in the Q class and can ONLY be accessed from within that class. The following compiler error results:
Tester.java:17: privateVar has private access in def.Q
privateVar = 5; // Line 5
^
packageVar = 6; // Line 6
As was true for line 2, package members are only accessible to classes within the same package as the class that declares them. Here we are attempting to access the packageVar member contained in an instance of the Tester class. In other words, an instance of Tester is trying to access the packageVar it contains by virture of its inheritance from Q. The protectedVar member was declared by a class in the def package and Tester is not in that package. Therefore, Tester cannot access the packageVar member. The following compiler error results:
Tester.java:18: packageVar is not public in def.Q; cannot be accessed from outside package
packageVar = 6; // Line 6
^
protectedVar = 7; // Line 7
There is no compiler error for line 7.
An instance of Tester can refer to its own copy of protectedVar, the copy of protectedVar contained in some other instance of Tester, or the copy of protectedVar contained in an instance of a subclass of Tester. An instance of Tester cannot, however, refer to a copy of protectedVar in an object that is not an instance of Tester.

publicVar = 8; // Line 8
As was the case with line 12, public members are available "everywhere". Consequently, Tester's reference to publicVar compiles without error.

t.privateVar = 9; // Line 9
As was the case with lines 1 and 5, the member, privateVar, is declared in class Q and can therefore only be accessed by instances of class Q, proper (i.e., non-subclasses of class Q). The following compiler error results:
Tester.java:25: privateVar has private access in def.Q
t.privateVar = 9; // Line 9
^
t.packageVar = 10; // Line 10
As was the case with lines 2 and 6, the member, packageVar, is declared in class Q which is in the def package. Only instances of classes in the same package as class Q can access the package variable, packageVar. Remember that because the Tester class does not have a package statement it is placed in Java's "default package". The following compiler error results:
Tester.java:26: packageVar is not public in def.Q; cannot be accessed from outside package
t.packageVar = 10; // Line 10
^
t.protectedVar = 11; // Line 11
You may want to refer back to the description of line 7. If we have a class (call it B) that contains a protected member (call it p) and another class (call it S) extends B, all instances of S can access protected members in any instance of class S, including subsclasses of S.
Once again, in this coded example, class B is class Q, class S is class Tester, and p is the protected member protectedVar. Line 11 is an example of an instance of Tester attempting to reference a protected member in another instance of Tester (represented by the member, t). Since an instance of Tester is attempting to reference a protected member in another instance of Tester, the line compiles cleanly.

t.publicVar = 12; // Line 12
As was the case with lines 4 and 8, public members are available "everywhere". Consequently, Tester's reference to t.publicVar compiles without error.

sub.privateVar = 13; // Line 13
As was the case with lines 1, 5, and 9, the member, sub.privateVar, is declared in class Q and can therefore only be accessed by instances of class Q, proper (i.e., non-subclasses of class Q). The following compiler error results:
Tester.java:32: privateVar has private access in def.Q
sub.privateVar = 13; // Line 13

sub.packageVar = 14; // Line 14
As was the case with lines 2, 6, and 10, the member, sub.packageVar, is declared in class Q which is in the def package. To access this member the class attempting the access (Tester, in this case) must be in the def package. Remember that since Tester does not have a package statement it is placed in Java's "default package". The following compiler error results:
Tester.java:33: packageVar is not public in def.Q; cannot be accessed from outside package
sub.packageVar = 14; // Line 14
^
sub.protectedVar = 15; // Line 15
You may want to refer back to the descriptions of lines 7 and 11. Actually, this line compiles cleanly for exactly the same reason line 11 compiled cleanly. Remember that class Sub extends class Tester. Consequently, any instances of Sub are also considered instances of Tester.

sub.publicVar = 16; // Line 16
As was the case with lines 4, 8 and 12, public members are available "everywhere". Consequently, Tester's reference to sub.publicVar compiles without error.

Answers: 2 (b)
The static method addWidget cannot access the member variable wName. Static methods can refer to static variables only, such as widgetCount.
Answers: 3 (a)
Because the scope of the tmp String is confined to the try block, thus, it cannot be used in line 6. answer (c) would not occur even if the scope of the tmp variable were fixed. answer (d) would occur only if the scope of the tmp variable were fixed by declaring and initializing tmp in the first line of the method.

Answers: 4 (b)
This question is about the requirement to understand the difference between the "is-a" and the "has-a" relationship. Where a class is inherited you have to ask if it represents the "is-a" relationship. As the difference between the root and the two children are the operating system you need to ask are Linux and Windows types of computers.The answer is no, they are both types of Operating Systems. So option two represents the best of the options. You might consider having operating system as an interface instead but that is another story.
Of course there are as many ways to design an object hierarchy as ways to pronounce Bjarne Strousjoup, but this is the sort of answer that Sun will probably be looking for in the exam. Questions have been asked in discussion forums if this type of question really comes up in the exam. I think this is because some other mock exams do not contain any questions like this. I assure you that this type of question can come up in the exam. These types of question are testing your understanding of the difference between the is-a and has-a relationship in class design.

Answers: 5 (d)
Any call to this or super must be the first line in a constructor. As the method already has a call to this, no more can be inserted.

Answers: 6 (a), (b)
Answer (a) is correct because the ApDerived class inherits from ApBase, which implements Runnable. Answer (b) is also correct because the inserted cast (Runnable) is not needed but does not cause a problem. Answer (c) fails to compile because the compiler can tell that the ApBase class does not implement Observer. Answer (d) compiles, but fails to execute. Because of the specific cast the compiler thinks you know what you are doing, but the type of the aBase reference is checked when the statement executes and a ClassCastException is thrown.

Answers: 7 (d)
These casts and assignment are all legal. (a) is incorrect incorrect because an object reference can be assigned to an interface reference as long as the compiler knows that the object implements the interface. Answer (b) is incorrect because an interface reference can be assigned to a reference to Object because Object is the base of the Java class hierarchy. Answer (c) is incorrect because the object referred to has not lost its identity, so it passes the runtime cast check.

Answers: 8 (b)
It compiles and runs, the compiler assumes you know what you are doing with the cast to Runnable. Answer (a) fails to compile. As far as the compiler is concerned, obj is a plain Object so it objects to the assignment to a Runnable reference. Answer (c) compiles but fails to run. Because of the specific cast, the compiler thinks you know what you are doing, but the type of the aBase reference is checked when the statement executes, and a ClassCastException is thrown. Answer (d) fails to compile. As far as the compiler is concerned, obj is a plain Object so it objects to the assignment to an Observer reference.

Answers: 9 (c)
Without the cast to sub you would get a compile time error. The cast tells the compiler that you really mean to do this and the actual type of b does not get resolved until runtime. Casting down the object hierarchy as the compiler cannot be sure what has been implemented in descendent classes. Casting up is not a problem because sub classes will have the features of the base classes. This can feel counter intuitive if you are aware that with primitives casting is allowed for widening operations (ie byte to int).

Answers: 10 (d)
Using the package statement has an effect similar to placing a source file into a different directory. Because the files are in different packages they cannot see each other. The stuff about File1 not having been compiled was just to mislead, java has the equivalent of an "automake", whereby if it was not for the package statements the other file would have been automatically compiled.

Answers: 11 (b)
by overriding the oxygenConsumption method, the Java runtime will call the overriding method for all fish where a specific method is provided or the generic method if there is none. Answer (a) is incorrect because if you overloaded the oxygenConsumption method using a different method signature, the Java runtime would not call the specific method for Fish where a specific method was provided. It would always call the generic method.

Answers: 12 (d)
There is no way for a class outside the GenericFruit hierarchy to call the GenericFruit method using an Apple reference. Answer (a) is incorrect because the runtime resolution of method calls finds the Apple method. Answer (b) is incorrect because this extra cast does not change the object type. Answer (c) does not create a valid Java statement.

Answers: 13 (c)
As both the method in class Child and Parent are static the method call is bound at compile time with the class whose reference variable is taken i.e. Parent.

Answers: 14 (d)
Whenever a method/Constructor has an argument which matches two different methods/Constructors definations then it will always call the most specific one. As in our case Object is a general class and is super class of all other classes so it will call the String version of the Constructor.

Answers: 15 (a)
As in this case both classes are peer i.e. both the classes is at the same level in the hierarchy chart.

Answers: 16 (d)
Method are bound at run time where as variables are bound at compile time.It is the type of object which will tell that which method is to be called(i.e. B in this case), but it is the type of reference which will tell you which variable is to be called(i.e. A in this case).

Answers: 17 (b), (c)
The program will not compile as the there is no method named getFields() defined in the class Base.It will compile if we cast the reference variable a for the object of class Agg like ((Agg) a).getFields()

Previous Home Next
Interview Questions And Answers
Struts interview questions and answers (Subjective)
500 Java Objective Questions and Answer
Core Java Objective Questions And Answers
Core Java Subjective Questions And Answers
Core Java Interview Questions And Answers
Core Java Interview Questions and Answers (Subjective)
Core Java Interview Questions and Answers( Objective)
50 Servlet interview questions
155 Java Interview Questions
EJB Interview Questions and Answers(Subjective)
R4R,JSP Interview Questions and Answer(Subjective)
R4R,Java Servlets Interview Questions and Answers(Subjective)
Core Java Subjective ,Objective and Interview Questions And Answers
275 Core java interview questions
Java Objective Questions and Answer
Java Architect Interview Questions
Applet Interview Questions and Answers
Core Java example
Servlet Objective Questions And Answers
Servlet Subjective Questions And Answers
Servlet Interview Questions And Answers
JSP Objective Questions And Answers
JSP Subjective Questions And Answers
JSP Interview Questions And Answers

Contact US:

Your Name:


Your Email:

Message:

Comments:

Give Your Comments:


Advertiser PRIVACY POLICY ||User PRIVACY POLICY || R4R Group Srvices