Java Objective Questions and Answer
Java Objective Questions and Answer
com
CLASS XII
GUESS PAPER
INFORMATICS PRAXCTICES
Java Objective Questions and Answer
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Q12.What will happen when you attempt to compile and run the following code?
class Base{
public void Base(){
System.out.println("Base");
}
}
public class Inner extends Base{
public static void main(String argv[]){
Inner i=new Inner();
}
}
a) Compile time error Base is a keyword.
b) Compilation and no output at runtime.
c) Output of Base.
d) Runtime error Base has no valid constructor.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Q13.You have a public class called myclass with the main method defined as follows
public static void main(String parm[]) {
System.out.println(parm[0]);
}
If you attempt to compile the class and run the program as follows
“java myclass hello”
What will happen?
a) Compile time error, main is not correctly defined.
b) Run time error, main is not correctly defined.
c) Compilation and output of java.
d) Compilation and output of hello.
Q17.What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Inner extends Base{
public static void main(String argv[]){
Inner s = new Inner();
}
void Inner (){
System.out.println("Inner");
}
}
a) Compilation and output of the string “Inner “at runtime.
b) Compile time error.
c) Compilation and no output at runtime.
d) Compilation and output of the string "Base".
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
a) A static methods do not have access to the implicit variable called this.
b) A static method may be called without creating an instance of its class.
c) A static method may not be overridden to be non-static.
d) A static method may not be overloaded.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
number result.
c) The following statement will produce a result of zero,
System.out.println(1 >>1);
d) All the Java integral types are signed numbers
Questions 1. Which of these array declarations and instantiations are not legal?
Select all valid answers:
(a) int []a[] = new int[4][4];
(b) int a[][] = new int[4][4];
(c) int a[][] = new int[][4];
(d) int []a[] = new int[4][];
(e) int[][] a = new int[4][4];
(f) int [ ] [ ] a = new int[4][4];
Questions 2 Which of these array declarations and initialization are not legal?
Select all valid answers:
(a) int []i[] = {{1,2}, {1}, {}, {1,2,3}};
(b) int i[]= new int[2]{1, 2};
(c) int i[][] = new int[][]{{1,2,3}, {4,5,6}};
(d) int i[][] = {{1, 2}, new int[2]};
(e) int i[4] = {1, 2, 3, 4};
Questions 3 Given the following code, which statements can be placed at the indicated position
without causing compilation errors?
public class ThisUsage {
int planets;
static int suns;
Questions 4 Given the following pairs of method declarations, which of these statements ar true?
void fly(int distance) {}
int fly(int time, int speed) {return time*speed}
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Questions 5 Given a class named Book, which of these would be valid definitions of constructors for
the class? [1]
Select all valid answers:
(a) Book(Book b) {}
(b) Book Book() {}
(c) private final Book() {}
(d) void Book() {}
(e) public static void Book(String args[]) {}
(f) abstract Book() {}
Questions 7 What will be the result of attempting to compile the following program?
Public class MyClass {
Long var;
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Questions 8 Given the following class, which of these are valid ways of referring to the class from
outside of the package net.basemaster?
package net.basemaster;
Questions 9 Which one of the following class definitions is a legal definition of a class that cannot be
instantiated?
Select the one right answer:
(a) class Ghost {
abstract void haunt()
}
(b) abstract class Ghost {
void haunt();
}
(c) abstract class Ghost {
void haunt() {};
}
(d) abstract Ghost {
abstract void haunt();
}
(e) static class Ghost {
abstract haunt();
}
Questions 10 Which of these statements concerning the use of modifiers are true?
Select all valid answers:
(a) If no accessibility modifier (public, protected and private) is given in a member declaration of a
class, the member is only accessible for classes in the same package and subclasses of the class.
(b) You cannot specify visibility of local variables. They are always only accessible within the block in
which they ardeclared.
(c) Subclasses of a class must reside in the same package as the class they extend.
(d) Local variables can be declared static.
(e) Objects themselves don not have visibility, only references to the object.
Questions 11 Given the following source code, which one of the lines that are commeneted out may
be reinserted without introducing errors?
abstract class MyClass {
abstract void f();
final void g() {}
// final void h() {} // (1)
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
void f() {}
void h() {}
// void k() {i++;} // (3)
// void l() {j++;} // (4)
int m;
}
Select the one right answer:
(a) (1)
(b) (2)
(c) (3)
(d) (4)
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Questions 16 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();
}
(a) Error at compilation: native method cannot be static.
(b) Error at compilation native method must return value.
(c) Compilation but error at run time unless you have made code containing native amethod available.
(d) Compilation and execution without error.
Questions 17 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[]){
}
}
(a) Compile time error: Base cannot be private.
(b) Compile time error indicating that an integer cannot be transient.
(c) Compile time error transient not a data type.
(d) Compile time error malformed main method.
Questions 18 What will happen when you attempt to compile and run the following code?
public class Hope{
public static void main(String argv[]) {
Hope h = new Hope();
}
protected Hope() {
for(int i =0; i <10; i ++){
System.out.println(i);
}
}
}
(a) Compilation error: Constructors cannot be declared protected.
(b) Run time error: Constructors cannot be declared protected.
(c) Compilation and running with output 0 to 10.
(d) Compilation and running with output 0 to 9.
Questions 19 What will happen when you attempt to compile and run the following code with the
command line "hello there"?
public class Arg{
String[] MyArg;
public static void main(String argv[]){
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
MyArg=argv;
}
Questions 20 Which of the following method will not give you any error when placed on line no.3?
Select one correct answer.
1 interface i2
2{
3 //Here
4}
(a) static void abc();
(b) abstract void abc();
(c) native void abc();
(d) synchronized void abc();
(e) final void abc();
(f) private void abc();
(g) protected void abc();
Answers:1 (c)
The [] notation can be placed both before and after the variable name in an array declaration.
Multidimensional arrays are created by creating arrays that can contain references to other arrays. The
statement new int[4][] will create an array of length 4, which can contain references to arrays of int
values. The statement new int[4][4] will create the same array, but will also create four arrays, each
containing four int values. References to each of these arrays are stored in the first array. The
statement int [][4] will not work, because the dimensions must be created from left to right. Extra
spaces are not significant.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
not a part of the method signatures. Both methods in the first pair named fly and therefore overload
this method name. The methods in pair three do not overload the method name glide, since only one
method has the name. The method name Glide is distinct from the method name glide, as identifiers in
Java are case-sensitive.
Answers:5 (a)
A constructor does not declare any return type, not even void. A constructor cannot be final, static or
abstract.
Answers:7 (c)
A compilation error will be encountered in (3), since the class does not have a constructor accepting a
single argument of type int. The declaration at (1) declares a method, not a constructor, since it have
a return type. The method happens to have the same name as the class, but that is irrelevant. The
class has an implicit default constructor since the class contains no constructor declarations. This
allows the instantiation at (2) to work.
Answers:9 (C)
A class is uninstantiable if the class is declared abstract. The declarartion of an abstract method cannot
provide an implementation. The declaration of a non-abstract method must provide an implemenation.
If any method in a class is declared abstract, then the whole class must be declared abstract.
Definition (d) is not valid, since it omits the class keyword.
Answers:11 (c)
The line “void k() { i++; }” can be reinserted without introducing errors. Reinserting line (1) will cause
the compilation to fail, since MyOtherClass will try to override a final method. Reinserting line(2) will
fail since MyOtherClass will no longer have a default constructor. The main() method needs a
constructor that takes zero arguments. Reinserting line (3) will work without any problems, but
reinserting line (4) will fail, since the method will try to access a private member of the superclass.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Answers:12 (b)
The keyword this can be only used in non-static methods. Only one instance of each static member
variable of a class is created. This instance is shared among all objects of the class. Local variables are
only accessible within the local scope, regardless of whether the local scope is defined within a static
method.
Answers:13 (a)
The transient keyword signifies that the variables should not be stored when the object are serialized.
Constructors cannot be declared abstract. Elements in an unitialized array object get the default value
corresponding to the type of the elements. Whether the reference variable pointing to the array object
is a local or a member variable does not matter. Abstract methods from a superclass need not be
implemented by abstract subclass.
Answers:16 (d)
It would cause a run time error if you had a call to amethod though.
Answers:17 (a)
A top level (non nested) class cannot be private.
Answers:18 (d)
Answers:19 (a)
You will get an error saying something like "Cant make a static reference to a non static variable". Note
that the main method is static. Even if main was not static the array argv is local to the main method
and would thus not be visible within amethod.
Answers:20 (b)
Interface methods can't be native, static, synchronized, final, private, or protected.
Questions .1 Which statements about the output of the following program are true?
public class Logic {
public static void main(String args[]) {
int i = 0;
int j = 0;
boolean t = true;
boolean r;
r = (t && 0<(i+=1));
r = (t && 0<(i+=2));
r = (t && 0<(j+=1));
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
r = (t || 0<(j+=2));
System.out.println( i + “ ” + j );
}
}
(a) The first digit printed is 1.
(b) The first digit printed is 2.
(c) The first digit printed is 3.
(d) The second digit printed is 1.
(e) The second digit printed is 2.
(f) The second digit printed is 3.
Questions 2 Which statements about the output of the following program are true?
public static void main(String args[])
{
int i =0;
i = i++;
System.out.println(i);
}
(a) 0 is printed.
(b) 1 is printed.
Questions 3 Which statements about the output of the following program are true?
public class EqualTest {
public static void main(String args[]) {
String s1 = “YES”;
String s2 = “YES”;
if ( s1 == s2 ) System.out.println(“equal”);
String s3 = new String(“YES”);
String s4 = new String(“YES”);
if ( s3 == s4 ) System.out.println(“s3 eq s4”);
}
}
(a) “equal” is printed, “s3 eq s4” is printed.
(b) “equal” is printed only.
(c) “s3 eq s4” is printed only.
(d) Nothing is printed.
Questions 4 What happens when you try to compile and run the following code?
public class EqualsTest {
public static void main(String args[]) {
char A = „\u0005‟;
if ( A == 0x0005L )
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);
}
}
(a) The compiler reports “Invalid character in input” in line 3
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Questions 5 What will happen when you attempt to compile and run the following code?
public class As{
int i = 10;
int j;
char z= 1;
boolean b;
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
}//End of another
}
(a) 10,0,30
(b) 20,0,30
(c) 20,99,30
(d) 10,0,20
Questions 7 Here are three proposed alternatives to be used in a method to return false if the object
reference x has the null value. Which statement will work?
(a) if ( x == null ) return false;
(b) if ( x.equals(null) ) return false;
(c) if ( x instanceof null ) return false;
Questions 8 What will be the result of compiling and running the given program?
Select one correct answer.
1 class Q1
2{
3 public static void main(String arg[])
4{
5 int a[]={2,2};
6 int b=1;
7 a[b]=b=0;
8 System.out.println(a[0]);
9 System.out.println(a[1]);
10 }
11 }
Questions 9 What will be the result of compiling and running the given program?
Select one correct answer.
1 public class Q4
2{
3 public static void main(String[] args)
4{
5 boolean t1 = true, t2 = false, t3 = t1;
6 boolean t = false;
7 t &&= (t1 || ( t2 && t3));
8 System.out.println(t);
9}
10 }
(a) Program compiles correctly and print true when executed.
(b) Program compiles correctly and print false when executed.
(c) Compile time error.
(d) Run time error.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Questions 10 What will be the result of compiling and running the given program?
Select one correct answer.
1 class AA{}
2 class BB extends AA{}
3 class Q6
4{
5 public static void main(String arg[])
6{
7 AA a=null;
8 BB b=(BB)a;
9 System.out.println(b);
10 System.out.println(b instanceof BB);
11 System.out.println(b instanceof AA);
12 }
13 }
(a) Program compiles correctly and print null,true,false.
(b) Program compiles correctly and print null,true,true.
(c) Program compiles correctly and print null,false,false.
(d) Program compiles correctly and print null,false,true.
(e) Compile time error at line no.8 as null value cannot be casted.
(f) Run time error at line no. 8 as null value cannot be casted.
Answers: 2 (a)
To explain u in a more simple way...
int k = 10;
int j;
Post increment :
j = k++;
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Pre-increment :
j = ++k;
In this case, k is having 10 as value, then the pre-increment
operation is being performed, so k becomes 11 in this case, & then it
is being assigned to j, so now j becomes 11 & k also becomes 11.
The thing which u have must have noticed in the post increment & pre-
increment is that the value of k is increased by 1, where as j in the
first case becomes 10 & in second case j becomes 11.
Answers: 3 (b)
The compiler creates one String object for both s1 and s2, thus “equal” appears. But using the new
String operator two distinct objects are created so “s3 eq s4” does not appear.
Answers: 4 (c)
The compiler promotes variable A to a long before the comparison. The compiler does not report
“Invalid character in input” in line 3 because this is correct form for initializing a char primitive.
Therefore, answer (a) is incorrect. Because (b) cannot possibly be correct.(c) is correct
Answers: 5 (a)
The default value for a boolean declared at class level is false, and integer is 0, and 1 is within the
range of the char and don‟t need casting.
Answers: 6 (d)
In the call another(v,i);
A COPY of the reference to v is passed and thus any changes will be intact after this call.
Answers: 7 (a)
The ONLY correct way to check a reference for the null value. Answer (b) is incorrect because if x is
null, there will not be an object whose equals method can be called. This statement would cause a
NullPointerException at runtime when x is null. Answer (c) is incorrect because only a reference type,
such as a class, or array, can be the right operand of the instanceof operator.
Answers: 8 (c)
First of all value of b which is 1 is assigned to array index then rest of the code will be executed.
Answers: 9 (c)
Compile time error: There is no operator like &&=
Answers: 10 (c)
As null value can be casted but do remember it is of no use to cast null value that is why it is written in
Khalid Mughal's book that we can not cast null value.Secondly,instanceof operator always return false
for null value.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
Language fundamentals
Questions 4 Which one of the following are not valid character contants?
Select any two.
(a) char c = '\u00001' ;
(b) char c = '\101';
(c) char c = 65;
(d) char c = '\1001' ;
Answers: 2 (a)
Although nonsensical, an empty file ia a valid source file. A source file can contain an optional package
declaration, any number of import statements and any number of class and interface definitions.
Answers: 3 (d)
A valid declaration of the main() method must be public and static, have void as return type and take a
single array of String objects as arguments. The order of the static and public keywords is irrelevant.
Also, declaring the method final does not affect the method‟s potential to be used as a main() method.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
public class Q {
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
}
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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);
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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 {}
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();
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
}
}
(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 }
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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 }
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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.
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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.
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 operati ng system you need to ask are Linux
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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: 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
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News
CBSEGuess.com
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).
--------------------------------------------------------------------------------------------------------
CBSE Sample Papers | CBSE Guess Papers | CBSE Practice Papers | Important Questions | CBSE PSA | CBSE OTBA |
Proficiency Test | 10 Years Question Bank | CBSE Guide | CBSE Syllabus | Indian Tutors | Teacher' Jobs CBSE eBooks |
Schools | Alumni | CBSE Results | CBSE Datesheet | CBSE News