Right Place For Right PersonTM 
Sponsored Ads
Buy This Website(This website is for Sale)
Home Tutorials Articles Forums Source Code Books Certifications Interviews Questions

Previous Home Next

Sun Java Certification Program

Inner classes

Questions 1 What will happen when you attempt to compile and run this program?
public class Outer {
public String name = "Outer";
public static void main(String argv[]) {
Inner i = new Inner();
i.showName();
}//End of main

private class Inner {
String name =new String("Inner");
void showName() {
System.out.println(name);
}
}//End of Inner class
}
(a) Compile and run with output of "Outer".
(b) Compile and run with output of "Inner".
(c) Compile time error because Inner is declared as private.
(d) Compile time error because of the line creating the instance of Inner.

Questions 2 Which of the following statements are true?
(a) An inner class may be defined as static.
(b) There are NO circumstances where an inner class may be defined as private.
(c) An anonymous class may have only one constructor.
(d) An inner class may extend another class.

Questions 3 Given the following class definition, which of the following statements would be legal after the comment //Here? [7]
class InOut{
String s= new String("Between");
public void amethod(final int iArgs) {
int iam;
class Bicycle{
public void sayHello(){
//Here
}//End of bicycle class
}
}//End of amethod
public void another(){
int iOther;
}
}
(a) System.out.println(s);
(b) System.out.println(iOther);
(c) System.out.println(iam);
(d) System.out.println(iArgs);

Questions 4 Which of the following statements are true?
(a) Adding more classes via import statements will cause a performance overhead, only import classes you actually use.
(b) Under no circumstances can a class be defined with the private modifier.
(c) A inner class may under some circumstances be defined with the protected modifier.
(d) An interface cannot be instantiated.

Questions 5 The following is an outline of code for top-level class. Assume that both classes have correct constructors taking no parameters.
class NormalClass {
static class NestedClass {
// methods and variables of NestedClass
}
// methods and variables of NestedClass
}
Which of the following code fragments shows the correct way to declare and initialize a reference to a NestedClass object?
(a) NormalClass.NestedClass myNC = new NormalClass.NestedClass();
(b) NestedClass myNC = new NormalClass().new NestedClass();
(c) NestedClass myNC = new NormalClass.NestedClass();

Questions 6 The following is an outline of code for top-level class. Assume that both classes have correct constructors taking no parameters.
class BaseClass {
static class NestedClass {
// methods and variables of NestedClass
}
// methods and variables of BaseClass
}
Which of the following code fragments shows the correct way to declare and initialize a reference to a NestedClass object?
(a) BaseClass.NestedClass myNC = new BaseClass.NestedClass();
(b) NestedClass myNC = new BaseClass().new NestedClass();
(c) NestedClass myNC = new BaseClass.NestedClass();
(d) BaseClass.NestedClass nbc = new BaseClass().new NestedClass();

Questions 7 Which of the following statements about this code are true?
public class Morecombe {
public static void main(String argv[]){
Morecombe m = new Morecombe();
m.go(new Turing(){});
}
public void go(Turing t){
t.start();
}
}
class Turing extends Thread {
public void run(){
for(int i =0; i < 2; i++){
System.out.println(i);
}
}
}
(a) Compilation error due to malformed parameter to go method.
(b) Compilation error, class Turing has no start method.
(c) Compilation and output of 0 followed by 1.
(d) Compilation but runtime error.

Answers:1 (d)
This looks like a question about inner classes but it is also a reference to the fact that the main method is static and thus you cannot directly access a non static method. The line causing the error could be fixed by changing it to say
Inner i = new Outer().new Inner();
Then the code would compile and run producing the output "Inner"

Questions 2 (a), (d)
A static inner class is also sometimes known as a top level nested class. There is some debate if such a class should be called an inner class. I tend to think it should be on the basis that it is created inside the opening braces of another class. How could an anonymous class have a constructor?. Remember a constructor is a method with no return type and the same name as the class. Inner classes may be defined as private

Questions 3 (a), (d)
A class within a method can only see final variables of the enclosing method. However it the normal visibility rules apply for variables outside the enclosing method.

Questions 4 (c), (d)
The import statement allows you to use a class directly instead of fully qualifying it with the full package name, adding more classess with the import statement does not cause a runtime performance overhad. I assure you this is true. An inner class can be defined with the private modifier.

Questions 5 (a)
(a) shows both correct reference variable declaration and a correct constructor. (b), (c) are incorrect declaration of the reference variable because the name of a nested class starts with the enclosing class name.

Questions 6 (d)
(d) shows both correct reference variable declaration and a correct constructor. This statement creates a BaseClass object, which the NestedClass must have. (b), (c) are incorrect declaration of the reference variable because the name of a nested class starts with the enclosing class name. Answer (a) does not create a BaseClass object for the NestedClass to be associated with.

Questions 7 (c)
The creation of an anonymous class as a parameter to go is fairly strange as you would expect it to override a method in its parent class (Turing). You don't have to though. The fact that class Turing extends Thread means the anonymous instance that is passed to go has a start method which then calls the run method.

Previous Home Next
Sponsored Ads
Sponsored Ads
Sponsored Ads
Sponsored Ads
Sponsored Ads
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
JDBC Objective Questions And Answers
JDBC Subjective Questions And Answers
JDBC Interview Questions And Answers
JDBC Interview questions and answers
Networking Interview questions and answers
Servlets Interview questions and answers
EJB Objective Questions And Answers
EJB Subjective Questions And Answers
EJB Interview Questions And Answers
Hibernate Objective Questions And Answers
Hibernate Subjective Questions And Answers
Hibernate Interview Questions And Answers
Spring Objective Questions And Answers
Spring Subjective Questions And Answers
Spring Interview Questions And Answers
Struts Objective Questions And Answers
Struts Subjective Questions And Answers
Struts Interview Questions And Answers
Ant Objective Questions And Answers
Ant Subjective Questions And Answers
Ant Interview Questions And Answers
PHP Objective Questions And Answers
PHP Subjective Questions And Answers
PHP Interview Questions And Answers 1
ASP.net Objective Questions And Answers
ASP.net Subjective Questions And Answers
ASP.net Interview Questions And Answers 1
ASP.net interview questions ,ASP.net interview answers,ASP.net interview questions and answers
PHP interview questions ,PHP interview answers,PHP interview questions and answers
Testing Objective Questions And Answers
Testing Subjective Questions And Answers
Testing Interview Questions And Answers
Ajax Tutorials
Ajax Objective Questions And Answers
Ajax Subjective Questions And Answers
Ajax Interview Questions And Answers
Linux Objective Questions And Answers
Linux Subjective Questions And Answers
Unix Subjective Questions And Answers
Unix Interview Questions And Answers
HR interview questions and answers
HR Interview Questions Tips
HR Objective Questions And Answers
HR Subjective Questions And Answers
HR Interview Questions And Answers
Learn C Language with in a day
 C /C++ Questions
C Objective Questions And Answers
C Subjective Questions And Answers
C Interview Questions And Answers
C Objective Interview Questions And Answers(10)
C Subjective Interview Questions And Answers(100) 
C syntax,semantics and simple programming questions(61) 

Linux Interview Questions And Answers
Unix Objective Questions And Answers
C Aptitude Questions(179)
C++ Interview Questions And Answers    
C++ Objective Questions And Answers
C++ Subjective Questions And Answers
C++ Interview Questions And Answers
C,C++ objective Interview Questions and answers
C Interview Questions And Answers( Objective and Subjective)
C Objective Interview Questions And Answers(10)

C Subjective Interview Questions And Answers(100)

C,C++ Interview Questions

35 C++ Interview Questions And Answers(Subjective)

109 C++ Interview Questions

Java Script Interview Question and Answer(1-10).
 Java Script Interview Question and Answer(11-20).
Java Script  Interview Question and Answer(21-30).
Java Script Interview Question and Answer(31-40).
Javascript Objective Questions And Answers
Javascript Subjective Questions And Answers
Javascript Interview Questions And Answers
MFC Interview Questions And Answers(Subjective)

MFC Interview Questions And Answers(Subjective)

ATL Interview Questions And Answers(Subjective)

COM DCOM Interview Questions And Answers(Subjective)

Win32API Interview Questions And Answers(Subjective)

ActiveX Interview Questions And Answers(Subjective)

R4R, VC++ AllOther Interview Questions And Answers(Subjective)

PL/SQL Interview Questions And Answers(Subjective)

152 PL/SQL Interview Questions And Answers(Subjective)

Asp Objective Questions And Answers
Asp Subjective Questions And Answers
Asp Interview Questions And Answers 1

VB Objective Questions And Answers

Contact US:

Your Name:


Your Email:

Message:

Comments:

Give Your Comments:


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