You are on page 1of 3

Test 3 – Jan 2020

Marks : 15

1. What will be output of following code.

public class Out_1 {


public static void main(String[] args) {
A a = new A(4,"abc");
System.out.println(a.i + a.s);
} }
class A {
int i ;
String s ;
A(int i , String str ){
i=i ;
s=str;
}}

2. What will be outout of following code.

public class Out_2 {


public static void main(String[] args) {
Parent ch1 = new Child();
System.out.println(ch1.num);
ch1.show();
}}

class Parent {
int num = 100;
public void show() {
System.out.println(" I am parent");
}}

class Child extends Parent {


int num = 200;
public void show() {
System.out.println(" I am child");
}}
3. What will be output of following code.

public class Out_3 {


public static void main(String[] args) {
AB ab = new AB();
ab.showVar();
}}

class AB {
int rollNo;
String name;

public static void showVar() {


System.out.println(" roll no= " + rollNo + " name=" + name);
}}
4. What will be output of following.

package com.tests.test3;

public class Out_4 {


public static void main(String[] args) {
D d = new D("Blue");
System.out.println(d.color);
}}

class C {
int id;
String name;

C(int id) {
this.id = id;
}}

class D extends C {
String color;
String type;

D() {
this.color = "Red"; }

D(String color) {
this();
this.color = color;
} }

5. What will be output of following code.


package pack1;
public class Parent {
int num ;
Parent(){
System.out.println("I am in parent");
} }

package pack2;
import pack1.Parent;
public class B extends Parent{
public int foo() {
int i = 10 ;
return(this.num + i); }

public static void main(String[] args) {


B b = new B();
System.out.println(b.foo());
}}

6. State true or false. (0.25 marks will be cut for wrong answer) [3M]
a. Abstract class must have at least one abstract method.
b. Instance variables can not be accessed in static methods.
c. Overloading is example of runtime polymorphism.
d. We can override abstract methods.
e. We can override static methods.
f. Protected variables are not accessible to classes in same package if that class does not
extend.

7. Create a java class Cricketer. Class has int playerid, String name , Boolean isBaller , float
strikerate , int runs. All fields are private. Write getter setter for all fields of this class.
8. Write a main method in which you accept values from user to create 3 objects of cricketer
and display them. To display method you should use tostring method.

9. Create class Fish { String name , int gills } . Class Fish has method void swim() which has
some default implementation. Create two subclasses of Fish , GoldFish and Whale. Override
swim method in GoldFish by changing access modifier and Override in Whale class by
changing its return type.

10. Write a program to accept a number from user and generate following series till that
number. 1, 2 , 1 , 3 , 2 , 5 , 3, 8 , 5, 13 , 8 , 21,

11. Show 5 uses of ‘this’ keyword in code.

12. Show 3 uses of ‘super’ keyword in code.

13. What is significance of final keyword for variable, method and class.

You might also like