0% found this document useful (0 votes)
567 views

(XXXX) 2 Marks (XXXX) : - Class B Extends A

The code defines a Matrix class with a main method that initializes two 2D integer arrays, a and b, sums their elements, stores the results in a 2D array c, and prints c. It then asks 5 multiple choice questions about the code: (A) what would happen if b was initialized as a String array instead of int, (B) the output if c is printed instead of its elements, (C) the correct output, (D) the output if the inner for loop condition is changed, and (E) the output/error if b has a different size than a.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
567 views

(XXXX) 2 Marks (XXXX) : - Class B Extends A

The code defines a Matrix class with a main method that initializes two 2D integer arrays, a and b, sums their elements, stores the results in a 2D array c, and prints c. It then asks 5 multiple choice questions about the code: (A) what would happen if b was initialized as a String array instead of int, (B) the output if c is printed instead of its elements, (C) the correct output, (D) the output if the inner for loop condition is changed, and (E) the output/error if b has a different size than a.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

(XXXX) 2 MARKS (XXXX)

1.
class Building
{
Building()
{
System.out.println("Building");
}
Building(String name)
{
this();
System.out.println(“Building: String Constructor" + name);
}
}
public class House extends Building
{
House()
{
System.out.println("House ");
}
House(String name)
{
this();
System.out.println("House: String Constructor" + name);
}
public static void main(String[] args)
{
new House("XYZ");
}
}

(A)Which of these is correct way of inheriting class A by class B ?


• Class B + Class A {
• Class B inherits class A {}
• Class B extends A {}
• Class B extends classs A{}
(B)All classes in java are inherited from which class?

• Java.lang.class
• Java.class.inherited
• Java.class.object
• Java.lang.object
(C)What is the use of “this” method in above code ?
• This is used to initialize constructor
• This keyword refers to the parent object
• This keyword refers to the current object
• None
(D)What would be result if a class implements two interfaces and both have a method with same name
and signature? Lets assume that the class is not implementing that method.

• Runtime Error
• Compile time error
• Codes run successfully
• First call method is executed sucsessfully
(E) What is the output of above code?

• Building House House: String ConstructorXYZ


• Building House XYZ
• Building House House: String Constructor
• House House : String Constructor House : String ConstructorXYZ

2. (2 Marks)

class display {
int x;
void show() {
if (x > 1)
System.out.print(x + " ");
}
}
class packages {
public static void main(String args[]) {
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].x = 0;
arr[1].x = 1;
arr[2].x = 2;
for (int i = 0; i < 3; ++i)
arr[i].show();
}
}

(A)Packages that are insides another package are known as?

• Packages
• Nested Packages
• Util subpackages
• Subpackages
(B)What is the correct output of program?
• 012
• Compile time error
• 0
• 2
(C) Which of the following package stores all the standard java classes?
• Lang
• Java
• Java.packages
• Util
(D)What will be the output if if(x>1) will be changed as if (x<1) ?
• 012
• 2
• 0
• Compile time error
(E)What will be result if both for loop will be changed to for (int i=0;i>3;i++) ?
• Java.lang.NullpointerException
• 012
• Run time error
• 0

3.
class demo
{
int a, b;

demo()
{
a = 10;
b = 20;
}
public void print()
{
System.out.println ("a = " + a + " b = " + b + "\n");
}
}

class Test
{

public static void main(String[] args)


{
demo obj1 = new demo();
demo obj2 = obj1;

obj1.a += 1;
obj1.b += 1;

System.out.println ("values of obj1 : ");


obj1.print();
System.out.println ("values of obj2 : ");
obj2.print();

}
}

(A)What is true about private constructor? (2 Marks)


• Private constructor ensures only one instance of a class exist at any point of time
• Private constructor ensures multiple instances of a class exist at any point of time
• Private constructor eases the instantiation of a class
• Private constructor allows creating objects in other classes.

(B)Which of the following has the highest memory requirement? (2 Marks)


• Heap
• Stack
• JVM
• Class

(C)In order to restrict a variable of a class from inheriting to subclass. How variables should be declared? (2 Marks)
• Protected
• Private
• Public
• Static
(D)When object of class initialized, then which constructer to be called? (2 Marks)
• Copy constructer
• Default Constructor
• Parameterized Constructor
• None of these

(E) Which of this statement is incorrect? (2 Marks)


• All object of a class are allotted memory for the all the variables defined in the class
• If a function is defined public it can be accessed by object of other class by inheritation.
• main() method must be made public
• All object of a class are allotted memory for the methods defined in the class

4.
import java.io.*;
interface ln1
{
final int a = 10;
void display();
}
class TestClass implements ln1
{
public void display()
{
System.out.print("java");
}
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}

(A)Guess the result of your program if object creation will be like TestClass t= new ln1(); ? (2 Marks)
• Java 10
• In 1 is abstract; cannot be instantiated
• Runtime Error (NOT CONFIRM)
• Null

(B)A java interface can contain_______________? (2 Marks)


• Public static final variables only
• Public Abstract methods
• Abstract methods (unimplemented) and implemented methods both
• Public static final variable and abstract methods both

(C).What will be the output if value of a variable will be changed from 10 to 20 in child class? (2 Marks)
• Java 20
• Compile Time Error
• 20 (SORRY)
• Runtime error

(D) What is the correct output of program? (2 Marks)


• Java 10
• Compile time Error
• Java10
• Runtime Error

(E)What is need of interface? (2 Marks)


• To achieve total abstraction
• To achieve tight coupling
• Because interface can be instantiated directly
• To inherit parent progress

(XXXX) 1 MARKS (XXXX)

5.
class Matrix{
public static void main(String args[]){
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
(A)If I want to add String b[][]={{“priya”, ”teena” , “niti”}, {“reema”, “shruti” , “grany”} , { “tipsy”, “shainy”, “amrita”}}
with array a then What will be the result?
• Runtime Error
• Compile Time Error
• 1+priya 3+teena 4+niti 2+reena 4+shruti 3+granny 3+tipsy 4+shainy 5+amrita
• 1priya 3teena 4niti 2reema 4shruti
(B) What will be the output if instead of system.out.println(c[i][j]+” “) if system.out.println(c) is to be used out?
• Hashcode
• 468268469
• Compile time error
• Runtime Error
(C)What is the correct output of program?
• 268486469
• 268468469
• 268 469 486
• 468268469
(D)What will be the output if instead of for(int j=0;j<3;j++), for (int j=0; j>3; j++) will be used out?
• No output
• Compile Time Error
• Runtime Error
• 268469486
(E) If I want to add int b[][] ={{1,3,4}, {2,4,3}} with array a then what will be the output?
• Runtime Error
• Compile time error
• ArrayIndexOutofBoundsException (correct solution)
• Syntax Error

6.

Abstract class A {
Abstract void callme();

void callmetoo() {
System.out.println("This is a normal method.");
}}
class B extends A {

void callme() {

System.out.println("B's implementation of callme.");

}}

class XYZ {

public static void main(String args[]) {

B obj = new B();

obj.callme();

obj.callmetoo();

}}

(A)Abstract method having?


• Body written inside
• Body written outside
• No body
• All of the above
(B) Which of these is not abstract?
• Thread
• AbstractList
• List
• None of the mentioned.

(C)Abstract class provide?


• 0-100% abstraction
• 100% Abstraction
• No abstraction
• All of these
(D)Abstract class is used to create?
• Object,
• Abstraction
• Encapsulation
• Inheritance
(E)Abstract class having?
• Abstract Method
• Non- abstract method
• Both a and b
• None of these
7.

class ABC
{
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}

(A)Which of these are selection statements in java ?


• If ()
• For ()
• Continue
• Break
(B)What will be the output of the following java program ?
• 1357
• 2468
• 13579
• 123456789
(C)Which of these jump statements can skip processing the remember of the code in its body for a particular
Iteration?
• Break
• Return
• Exit
• Continue
(D)What is true about a break ?
• Break stops the execution of entire program
• Break halts the execution and forces the control out of the loop
• Break forces the control out of the loop and starts the execution of next iteration
• Break halts the execution of the loop for certain time frame

(E)Which of the following is not a valid jump statement ?


• Break
• Goto
• Continue
• Return

8.

class conversion {
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}

(A)What will be the output of the following java code?


• 38 43
• 39 44
• 295 300
• 295.04 300
(B)What is the numerical range of a char data type in java?
• -128 to 127
• 0 to 256
• 0 to 32767
• 0 to 65535
(C)If an expression contains double,int,float,long,then the whole expression will be promoted into which of these
data types?
• Long
• Int
• Double
• Float

(D) Which of these is necessary condition for automatic type conversion in java?
• The destination type is smaller than source type
• The destination type is larger than source type
• The destination type can be larger or smaller than source type
• None of the mentioned
(E)What is the range of bytes data type in java ?
• ( -128 to 127)
• -32768 to 32767
• (-2147483648 to 2147483647)
• None of the mentioned

You might also like