1. The concept of inheritance provides the idea of
Ans: reusability
2. package pkg1;
interface A{
void method1();
}
Ans: method1 visible outside package pkg1
3. If we want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
Ans: protected
4. What is the output of the following program?
class Foo{
public int a=3;
public void addFive(){
a+=5;
System.out.print("f");
}
}
class Bar extends Foo{
public int a=8;
public void addFive(){
this.a=5;
System.out.println("b");
}
}
public class TestClass{
public static void main(String[] args){
Foo f=new Bar();
f.addFive();
System.out.println(f.a);
}
}
Ans: b 3
5. class Parent{
public Parent(){
System.out.println("Parent");
}
}
class Child extends Parent{
public Child(){
System.out.println("Child");
}
}
public class Test{
public static void main(String args[]){
Parent parentObject=new Child();
}
}
What would be the output of the above program?
Ans: Parent Child
6. public interface Foo{
int k=4;
}
Which of the options are true
1. final int k=4;
2. public int k=4;
3. static int k=4;
4. abstract int k=4;
Ans: Statement 1, 2, 3 in the options are replace the statement int k=4;
7. Protected members of a base class are like________, but they may be accessed by derived classes.
Ans: private members
8. class SuperClass{
static void display(){
System.out.println("hello super class");
}
}
public class SubClass extends SuperClass{
static void display(){
System.out.println("hello sub class");
}
public static void main(String[] args){
SuperClass s=new SuperClass();
s.display();
SubClass sub=new SubClass();
sub.display();
s=sub;
s.display();
sub=s;
sub.display();
}
}
What is the output of the above program?
Ans: Compile time error
9. Given the following piece of code
public interface Guard{
void doYourJob();}
abstract public class Dog implements Guard{}
Which of the following statements is correct?
Ans: The code will compile without any errors.
10. A derived class may become a base class, if another class is derived from it
Ans: true
11. More than one class may be derived from a base class
Ans: true
12. In an inheritance situation, you may not pass arguments to a base class constructor
Ans: false
13. The base class access specification can be viewed as a filter that base class members must pass through when becoming inherited members of a derived class
Ans: true
14. When an interface is implemented by an abstract class, the abstract class must provide implementation for the methods present in the interface. State whether above statement is true or false?
Ans: false
**15. What is the output of this program?
class overload{
int x;
double y;
int add(int a, int b){
x=a+b;
}
int add(double c, double d){
y=c+d;
}
overload(){
this.x=o;
this.y=o;
}
}
class Overload_methods{
public static void main()
{
overload obj=new overload();
int a=2;
double b=3.2;
obj.add(a,a);
obj.add(b,b);
System.out.println(obj.x+"" + obj.y);
}
}
Ans: 6 6
**16. interface Standard{
public abstract void standard() throws Exception;
}
Select the correct implementation class for Standard interface
Ans: public class Television implements Standard throws Exception{public static void main(String args[]){Standard standard=new Television(); standard.Standard();} public void standard(){System.out.println("I am implementing standard");}}
17. Redefining the instance method of super class by sub class is
Ans: Method overriding
18. What is the output of the following program?
public class Greek{
int i=1;
public int get1(){
System.out.println("Super");
return i;
}
public static void main(String args[]){
Greek ga=new Arabik();
System.out.print(ga.i);
System.out.print(ga.get1());
}
}
class Arabik extends Greek{
int i=2;
public int get1(){
System.out.print("Sub");
return i;
}
}
Ans: 1Sub2
19. Which of these keywords is used to access the members of base class from a sub class?
Ans: super
20. Consider the following statements
i) Constructors can be abstract
ii) Static methods can be abstract
iii) Private methods can be abstract
Which statements are true?
Ans: All are false
21. What will be the output of the following program?
public class TestOverriding{
public static void main(String args[]){
Parrot bird=new Parrot();
bird fly(); //line 4
}
}
class Bird{
private void fly(){
System.out.println("Bird is flying");
}
}
class Parrot extends Bird{//line 12
public void doStuff(){
System.out.println("I am parrot, and I am doing stuff");
}
}
Ans: compilation error at line 4
22. Which of the following two keywords cannot come together when declaring a method?
i. static and final ii. final and abstract iii. static and abstract iv. abstract and private
Ans: All are correct
23. package pkg1;
interface A{
void method1();
}
Ans: method1 visible outside package pkg1
24. If we want subclasses in any package to have access to members of a superclass. What is the most restrictive access that accomplishes this objective?
Ans: protected
25. class X has a method:
int f1(int m){
return m*m;
}
class Y has a method:
String f1(int num){
return "abc";
}
Suppose Y is a subclass of X, then what type of polymorphism is being implemented in this scenario?
Ans: No Polymorphism. The code will have a compilation error
26. public interface Foo{
int k=4;
}
Which of the following options are true
1. final int k=4;
2. public int k=4;
3. static int k=4;
4. abstract int k=4;
Ans: Statement 1,2,3 in the options are replace the statement int k=4;
27. 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?
Ans: Compilation fails
**28. package abc;
public class Animal{
public Animal(){
System.out.println("inside animal's constructor");
}
public void printName(){
System.out.println("Animal");
}
}
package exam;
import abc.Animal;
public class Cat extends Animal{
public void printName(){
System.out.println("Cat");
}
}
package abc;
import exam.Cat;
public class Test{
public static void main(String[] args){
Animal a=new Cat();
}
}
What is the output of the given program?
Ans: Cat
**29. What will be the output of the below program?
public class Test
{
public static void main(String args[])
{
print(10,10);
}
private static void print(int i, long l)
{
System.out.println("Print integer first and then long");
}
}
Ans: Compilation error because of ambiguous method call
30. _____ ______myinter
{
int x;
abstract void display();
}
class A________myinter
{
public void display()
{
x=10;
System.out.println("Implementing display method");
}
}
Ans: Blank1-public, Blank2-interface, Blank3-implements
**31. What is the output of the following program
class Super{
public int getLength(){return 4;}
}
public class Sub extends Super{
public long getLength(){return 5;}
public static void main(String[] args){
Super sooper=new Super();
Sub sub=new Sub();
System.out.println("sooper.getLength()+","+sub.getLength());
}
}
Ans: Compilation fails
32. Which of the following is true?
Ans: An interface can extend many interfaces
33. Can we declare an abstract static method?
Ans: False
34. How do you define class named Exam1 so that it cannot be subclassed?
Ans: final class Exam1{}
35. Which of the following statements are correct?
1. Parent obj=new Child();
2. Parent obj=new Parent();
3. Child obj=new Child();
4. Child obj=new Parent();
Ans: Statement 1,2,3 are correct 4 is incorrect
36. How do you define class named Vehicle so that it cannot be subclassed?
Ans: final class Vehicle{}
37. What will be the output of the following code?
interface myinter1
{
void display();
}
interface myinter2
{
void print();
}
}
Select one or more true statements for above code
Ans: compilation error stating Cannot reduce the visibility of the inherited method from DisplayStack
39. Does following code displays a valid override?
class MyClass{
void add(int i,int ti){
//I will do later
}
}
class MySubclass extends MyClass{
public void add(int i,int ti){
//Will do now
}
}
Ans: true
40. What is the output of the below code?
interface myinter{
int x;
void display();
}
class A implements myinter
{
public void display()
{
System.out.println("Implementing display method");
}
}
Ans: compilation error
Ans: reusability
2. package pkg1;
interface A{
void method1();
}
Ans: method1 visible outside package pkg1
3. If we want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
Ans: protected
4. What is the output of the following program?
class Foo{
public int a=3;
public void addFive(){
a+=5;
System.out.print("f");
}
}
class Bar extends Foo{
public int a=8;
public void addFive(){
this.a=5;
System.out.println("b");
}
}
public class TestClass{
public static void main(String[] args){
Foo f=new Bar();
f.addFive();
System.out.println(f.a);
}
}
Ans: b 3
5. class Parent{
public Parent(){
System.out.println("Parent");
}
}
class Child extends Parent{
public Child(){
System.out.println("Child");
}
}
public class Test{
public static void main(String args[]){
Parent parentObject=new Child();
}
}
What would be the output of the above program?
Ans: Parent Child
6. public interface Foo{
int k=4;
}
Which of the options are true
1. final int k=4;
2. public int k=4;
3. static int k=4;
4. abstract int k=4;
Ans: Statement 1, 2, 3 in the options are replace the statement int k=4;
7. Protected members of a base class are like________, but they may be accessed by derived classes.
Ans: private members
8. class SuperClass{
static void display(){
System.out.println("hello super class");
}
}
public class SubClass extends SuperClass{
static void display(){
System.out.println("hello sub class");
}
public static void main(String[] args){
SuperClass s=new SuperClass();
s.display();
SubClass sub=new SubClass();
sub.display();
s=sub;
s.display();
sub=s;
sub.display();
}
}
What is the output of the above program?
Ans: Compile time error
9. Given the following piece of code
public interface Guard{
void doYourJob();}
abstract public class Dog implements Guard{}
Which of the following statements is correct?
Ans: The code will compile without any errors.
10. A derived class may become a base class, if another class is derived from it
Ans: true
11. More than one class may be derived from a base class
Ans: true
12. In an inheritance situation, you may not pass arguments to a base class constructor
Ans: false
13. The base class access specification can be viewed as a filter that base class members must pass through when becoming inherited members of a derived class
Ans: true
14. When an interface is implemented by an abstract class, the abstract class must provide implementation for the methods present in the interface. State whether above statement is true or false?
Ans: false
**15. What is the output of this program?
class overload{
int x;
double y;
int add(int a, int b){
x=a+b;
}
int add(double c, double d){
y=c+d;
}
overload(){
this.x=o;
this.y=o;
}
}
class Overload_methods{
public static void main()
{
overload obj=new overload();
int a=2;
double b=3.2;
obj.add(a,a);
obj.add(b,b);
System.out.println(obj.x+"" + obj.y);
}
}
Ans: 6 6
**16. interface Standard{
public abstract void standard() throws Exception;
}
Select the correct implementation class for Standard interface
Ans: public class Television implements Standard throws Exception{public static void main(String args[]){Standard standard=new Television(); standard.Standard();} public void standard(){System.out.println("I am implementing standard");}}
17. Redefining the instance method of super class by sub class is
Ans: Method overriding
18. What is the output of the following program?
public class Greek{
int i=1;
public int get1(){
System.out.println("Super");
return i;
}
public static void main(String args[]){
Greek ga=new Arabik();
System.out.print(ga.i);
System.out.print(ga.get1());
}
}
class Arabik extends Greek{
int i=2;
public int get1(){
System.out.print("Sub");
return i;
}
}
Ans: 1Sub2
19. Which of these keywords is used to access the members of base class from a sub class?
Ans: super
20. Consider the following statements
i) Constructors can be abstract
ii) Static methods can be abstract
iii) Private methods can be abstract
Which statements are true?
Ans: All are false
21. What will be the output of the following program?
public class TestOverriding{
public static void main(String args[]){
Parrot bird=new Parrot();
bird fly(); //line 4
}
}
class Bird{
private void fly(){
System.out.println("Bird is flying");
}
}
class Parrot extends Bird{//line 12
public void doStuff(){
System.out.println("I am parrot, and I am doing stuff");
}
}
Ans: compilation error at line 4
22. Which of the following two keywords cannot come together when declaring a method?
i. static and final ii. final and abstract iii. static and abstract iv. abstract and private
Ans: All are correct
23. package pkg1;
interface A{
void method1();
}
Ans: method1 visible outside package pkg1
24. If we want subclasses in any package to have access to members of a superclass. What is the most restrictive access that accomplishes this objective?
Ans: protected
25. class X has a method:
int f1(int m){
return m*m;
}
class Y has a method:
String f1(int num){
return "abc";
}
Suppose Y is a subclass of X, then what type of polymorphism is being implemented in this scenario?
Ans: No Polymorphism. The code will have a compilation error
26. public interface Foo{
int k=4;
}
Which of the following options are true
1. final int k=4;
2. public int k=4;
3. static int k=4;
4. abstract int k=4;
Ans: Statement 1,2,3 in the options are replace the statement int k=4;
27. 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?
Ans: Compilation fails
**28. package abc;
public class Animal{
public Animal(){
System.out.println("inside animal's constructor");
}
public void printName(){
System.out.println("Animal");
}
}
package exam;
import abc.Animal;
public class Cat extends Animal{
public void printName(){
System.out.println("Cat");
}
}
package abc;
import exam.Cat;
public class Test{
public static void main(String[] args){
Animal a=new Cat();
}
}
What is the output of the given program?
Ans: Cat
**29. What will be the output of the below program?
public class Test
{
public static void main(String args[])
{
print(10,10);
}
private static void print(int i, long l)
{
System.out.println("Print integer first and then long");
}
}
Ans: Compilation error because of ambiguous method call
30. _____ ______myinter
{
int x;
abstract void display();
}
class A________myinter
{
public void display()
{
x=10;
System.out.println("Implementing display method");
}
}
Ans: Blank1-public, Blank2-interface, Blank3-implements
**31. What is the output of the following program
class Super{
public int getLength(){return 4;}
}
public class Sub extends Super{
public long getLength(){return 5;}
public static void main(String[] args){
Super sooper=new Super();
Sub sub=new Sub();
System.out.println("sooper.getLength()+","+sub.getLength());
}
}
Ans: Compilation fails
32. Which of the following is true?
Ans: An interface can extend many interfaces
33. Can we declare an abstract static method?
Ans: False
34. How do you define class named Exam1 so that it cannot be subclassed?
Ans: final class Exam1{}
35. Which of the following statements are correct?
1. Parent obj=new Child();
2. Parent obj=new Parent();
3. Child obj=new Child();
4. Child obj=new Parent();
Ans: Statement 1,2,3 are correct 4 is incorrect
36. How do you define class named Vehicle so that it cannot be subclassed?
Ans: final class Vehicle{}
37. What will be the output of the following code?
interface myinter1
{
void display();
}
interface myinter2
{
void print();
}
interface myinter3 extends myinter1,myinter2
{
void show();
}
Ans: Yes, the code will compile successfully
38.
interface DisplayStack{
void display();
class Parent{
protected void display(){
System.out.println("parent display method");
{
void show();
}
Ans: Yes, the code will compile successfully
38.
interface DisplayStack{
void display();
class Parent{
protected void display(){
System.out.println("parent display method");
}
}
class Child extends Parent implements DisplayStack{
protected void display(){
System.out.pintln("Child display method");
}}
class Child extends Parent implements DisplayStack{
protected void display(){
System.out.pintln("Child display method");
}
Select one or more true statements for above code
Ans: compilation error stating Cannot reduce the visibility of the inherited method from DisplayStack
39. Does following code displays a valid override?
class MyClass{
void add(int i,int ti){
//I will do later
}
}
class MySubclass extends MyClass{
public void add(int i,int ti){
//Will do now
}
}
Ans: true
40. What is the output of the below code?
interface myinter{
int x;
void display();
}
class A implements myinter
{
public void display()
{
System.out.println("Implementing display method");
}
}
Ans: compilation error
Answer of question 9 is wrong,and the correct answer is option D
ReplyDeleteThank you for correcting my mistake. I will update it immediately.
Deletecan you post the materials of dotnet please
DeleteThank you for sharing this powerful article, your explanation is clear and very easy to understand. Please kindly visit our site to get more information about IT solution.SEO Services in Pakistan
ReplyDeletetons higher insights Top SEO Services Sites into the way your advertising efforts paintings. However what are assisted conversions, and why are they crucial? Hold studying to find out. Then join sales Weekly, our email publication, for greater digital advertising suggestions from the experts! Don’t miss our marketing manager Insider emails! Be a part of 200,000 clever entrepreneurs and get the month’s hottest marketing news and insights brought instantly on your inbox! Enter YOUR electronic mail below: input your paintings email post (Don’t fear, we’ll by no means proportion your statistics!) What are assisted conversions? Assisted conversions are SEO Arena any conversions that a given channel contributed to however
ReplyDelete