Java Practice Questions
***********************JAVA QUESTIONS*****************************
TOTAL QUESTIONS:-22
1. Why JAVA is called platform independent language?
2. What is JVM?
3. Give an example of Constructor Overloading in Java?
4. What is method overriding?
5. Is the following code an example of method overloading?
class a{
int b(int c,int d)
{
return 1;
}
double(int c,int d)
{
return 1;
}
}
6. What will be the output of following code fragment:-
class abc
{
abc(int a,int b)
{
System.out.println(a+b);
}
abc(int a)
{
System.out.println(a);
}
public static void main(String a[])
{
new abc(2,3);
new abc(2);
new abc();
}
}
7.What is the difference between Type Conversion And Type Casting give examples.
8.How runtime polymorphism is achieved in java?
9.Will the following code compile?
class b
{
public static int main(String a[])
{
System.out.println("this is the test");
}
}
10.What will be the output of following code:-
switch(2)
{
case 1:System.out.println("1");
case 2:System.out.println("2");
case 3:System.out.println("3");
default:System.out.println("default");
}
11.What will be the output of following code:-
switch(2)
{
case 1:System.out.println("1");
case 2:System.out.println("2");
case 3:System.out.println("3");
continue;
default:System.out.println("default");
}
12.Explain 'new' operator.
13.How many basic data types java provides.
14.Why "goto" is not used oftenly in most programming languages?
15. Given:
class Clidders {
public final void flipper() { System.out.println("Clidder"); }
}
public class Clidlets extends Clidders {
public void flipper() {
System.out.println("Flip a Clidlet");
super.flipper();
}
public static void main(String [] args) {
new Clidlets().flipper();
}
}
What is the result?
A. Flip a Clidlet
B. Flip a Clidder
C. Flip a Clidder
Flip a Clidlet
D. Flip a Clidlet
Flip a Clidder
E. Compilation fails.
16.Given:
public abstract interface Frobnicate { public void twiddle(String s); }
Which is a correct class? (Choose all that apply.)
A. public abstract class Frob implements Frobnicate {
public abstract void twiddle(String s) { }
}
B. public abstract class Frob implements Frobnicate { }
C. public class Frob extends Frobnicate {
public void twiddle(Integer i) { }
}
D. public class Frob implements Frobnicate {
public void twiddle(Integer i) { }
}
E. public class Frob implements Frobnicate {
public void twiddle(String i) { }
public void twiddle(Integer s) { }
}
17.Given:
class Top {
public Top(String s) { System.out.print("B"); }
}
public class Bottom2 extends Top {
public Bottom2(String s) { System.out.print("D"); }
public static void main(String [] args) {
new Bottom2("C");
System.out.println(" ");
} }
What is the result?
A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails.
18.Given:
class Clidder {
private final void flipper() { System.out.println("Clidder"); }
}
public class Clidlet extends Clidder {
public final void flipper() { System.out.println("Clidlet"); }
public static void main(String [] args) {
new Clidlet().flipper();
} }
What is the result?
A. Clidlet
B. Clidder
C. Clidder
Clidlet
D. Clidlet
Clidder
E. Compilation fails.
19.Given:
1. class Programmer {
2. Programmer debug() { return this; }
3. }
4. class Sssspublic class Foo {
void doStuff() { }
}
class Bar extends Foo {
void doStuff(String s) { }
}
The extends Programmer {
5. // insert code here
6. }
Which, inserted at line 5, will compile? (Choose all that apply.)
A. Programmer debug() { return this; }
B. SCJP debug() { return this; }
C. Object debug() { return this; }
D. int debug() { return 1; }
E. int debug(int x) { return 1; }
F. Object debug(int x) { return this; }
20.Given:
class Uber {
static int y = 2;
Uber(int x) { this(); y = y * 2; }
Uber() { y++; }
}
class Minor extends Uber {
Minor() { super(y); y = y + 3; }
public static void main(String [] args) {
new Minor();
System.out.println(y);
} }
What is the result?
A. 6
B. 7
C. 8
D. 9
E. Compilation fails.
F. An exception is thrown.
21.Given:
1. class Dog { }
2. class Beagle extends Dog { }
3.
4. class Kennel {
5. public static void main(String [] arfs) {
6. Beagle b1 = new Beagle();
7. Dog dog1 = new Dog();
8. Dog dog2 = b1;
9. // insert code here
10. }
11. }
Which, inserted at line 9, will compile? (Choose all that apply.)
A. Beagle b2 = (Beagle) dog1;
B. Beagle b3 = (Beagle) dog2;
C. Beagle b4 = dog2;
D. None of the above statements will compile
22.It is the example of overloading or overriding
public class Foo {
void doStuff() { }
}
class Bar extends Foo {
void doStuff(String s) { }
}
These questions are only for practice.If you want to know the answers e-mail me at javatute@gmail.com with your list of answers.
Some of these Questions are taken from the book of SCJP by Catherine Sierra and Bert Bates.


















0 comments:
Post a Comment
Post a Comment