Java Ques

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Shout4Education

PROBABLE QUESTIONS FOR JAVA EXAM


(MCQs)

1. Which if the following statements is/are FALSE?


a. ‘Package’ statement can appear anywhere in the java code
b. In a JAVA code one can only write one ‘import’ statement

n
i. Only (a) is false.
ii. Both (a) and (b) are false

io
iii. Only (b) is false.
iv. Both (a) and (b) are true

at
2. What will the output of following java code?

public class Application Tester{


public static void main(String[]args){
int[]array = new int[10];
System,out,println(array.length);
array[0] = 11;
array[1] = 22;
array[2] = 33;
uc
Ed
System,out,println(array.length);
}
}

i. Program will display 0 and 3


t4

ii. Program will display 10 and 10


iii. Program will display 10 and 3
iv. Program will display null and 3
ou

3. Which if the following statements is/are FALSE?


a. JVM is platform dependent.
b. Byte code is platform dependent.
Sh

i. Only (a) is false.


ii. Both (a) and (b) are false
iii. Only (b) is false.
iv. Both (a) and (b) are true

4. Which of the following statements are valid array declaration?


a. int number();
b. float average[];
c. double[] marks;
d. counter int[];

Shout4Education
Shout4Education
i. (a)
ii. (a) & (c)
iii. (d)
iv. (b) &(c)

5. Consider the java code given below and determine how many objects and
references are used in the code. Assume that student is a valid java class.

public class StudentTester{


public static void main(String[]args){

n
Student s1=new Student();
Student s2=new Student();

io
Student s3=s1;
Student s4=s1;
}
}

at
i. 2 objects 4 references
ii. 4 objects 2 references
iii.
iv.
2 objects 2 references
3 objects 4 references

6. What will be the output of following code:


uc
Ed
public class Test{
public static void main(String[]args){
int x=7;
switch(x){
t4

case 2:
System,out,println(”2”);
break;
ou

default: //line-1
System,out,println(“default”);
break;
case 3:
System,out,println(“3”);
Sh

break;
}
}
}

i. Compilation fails at line-1 because default case should be the last case
ii. Default
iii. 7
iv. Prints nothing

Shout4Education
Shout4Education
7. Consider the following java code:
public class SoftwareEngineer{
private String name;
private int empId;
public SofwareEngineer(String name, int empId){

this.empId=empId;
this.name=name;
}
}

n
Identify the correct java code which will help you to create an instance of
SoftwareEngineer. Choose the most appropriate answer.

io
i. SoftwareEngineer engineer = new SoftwareEngineer(“David”,1001);
ii. SoftwareEngineer engineer = new SoftwareEngineer();

at
iii. SoftwareEngineer engineer = new SoftwareEngineer(1001,”David”);
iv. SoftwareEngineer engineer = new SoftwareEngineer(“David”);

uc
8. You are required to create an interface called ‘Bank’ in which you must include two
methods ‘deposit’ and ‘withdraw’. Identify the CORRECT option which will meet
your requirement. Choose the most appropriate option.
Ed
i. public interface Bank{public double withdraw(double amount); public void
deposit(double amount);}
ii. public interface Bank{private double withdraw(double amount); private void
deposit(double amount);}
t4

iii. public interface Bank{protected double withdraw(double amount); protected


void deposit(double amount); }
iv. public interface Bank{public static double withdraw(double amount); public
static void deposit(double amount);}
ou

9. What will be the output of the following java code?


public class ApplicationTester{
public static void main(String[] args){
Sh

char ch='4';
switch(ch)
{
case '2':System.out.println("Hello");
break;
default: System.out.println("Hello");
case '1': System.out.println("Hello");
case '3': System.out.println("Hello");
break;
}
}
}

Shout4Education
Shout4Education

i. “Hello” will be displayed once.


ii. “Hello” will be displayed 3 times
iii. Compilation error: Invalid use of ‘switch’ statement
iv. “Hello” will be displayed 2 times

10. Which of the following is a VALID declaration of an abstract method? Choose the
most appropriate option.
i. public void drive();

n
ii. public abstract void drive();
iii. public abstract void drive(); {}
iv. public abstract drive(){}

io
11. Consider the below classes and identify how many car objects are created in the
heap?

at
Car.java

package learning;
public class Car{
private int distanceTraveled;
private int fuelConsumed;
//Getter and Setter methods
uc
Ed
}
CarService.java
package learning;
public class CarService{
public void calulateMileage(Car car)
{
t4

int mileage=
car.getDistanceTraveled()/car.getFuelConsumed();
System.out.println("Car Mileage="+mileage);
}
ou

CarClient.java

Package learning;
Sh

public class CarClient


{
public static void main(String args[])
{
CarService service=new CarService();
Car eon=new Car();
eon.setDistanceTraveled(3000);
eon.setFuelConsumed(30);
service.calculateMileage(eon);
}
}

Shout4Education
Shout4Education
i. 3
ii. 2
iii. 1
iv. 0
12. What will be the output of the following java code?

public class ConstructorTest{


private ConstructorTest(int w) {
System.out.println(w)
}

n
public static ConstuctorTest(){ //Line-6

System.out.println(10);

io
}
public static void main(String args[]) {
ConstructorTest obj=new ConstructorTest(50);
}

at
}

i. 10
ii. 10 50
iii.
iv.
50
uc
Won’t compile because od line-6, constructor can’t be static

13. What will be the output of the following java code?


Ed
package x;
public class Student{
void display(){ //Line-1
System.out.println("Hello")
}
t4

}
package y;
import x.Student; //Line-2
public class TestFinalClass{
ou

public static void main(String[] args) {


Student s= new Student();
s.display(); //Line-3
}
}
Sh

i. Compilation error at Line-3: The method display from type student is not
visible
ii. Compilation error at Line-2: Cannot import class Student from package x
iii. It will print Hello
iv. Compilation error at Line-1: No access specifier defined for method display

Shout4Education
Shout4Education
14. What will be the output of the following java code?

package pack1;
public class A{
private int i;
private int j;
protected A() {
i=10;
j=20;
}
public int getI() {
return i;

n
}
public void setI(int i) {

io
this.i=i;
}
public int getJ() {
return j;

at
}
public void setJ(int j) {
this.j=j;
}

}
public void disp()

}
{

uc
System.out.println(i+" "+j)

package pack2;
Ed
import pack1.A;
public class MyMain{
public static void main(String[] args) {
A obj1=new B();
obj1.setI(30);
obj1.setJ(40);
t4

A obj2=obj1;
obj2.setI(50);
obj1.disp();
obj2.disp();
ou

}
}

i. 30 40 50 40
ii. 50 40 50 40
Sh

iii. Compilation Error in line 1: Implicit super constructor A() is not visible. Must
explicitly invoke another constructor
iv. 30 40 50 20

Shout4Education
Shout4Education
15. Consider the following java code. What access specifier can be used for
computeSalary() method in Employee class, so that code in Line-1 will execute
without any error? Choose the most appropriate answer.
package myPackage;
public class Employee
{
private int empNo;
private double salary;
void computeSalary()
{
//logic to compute salary

n
}
}

io
package myPackage
public class PackageTester
{
public static void main(String[] args)

at
{
Employee emp=new Employee();
emp.computeSalary();
}
}
i.
ii.
iii.
Only public
Only private
uc
Either public or protected or default
Ed
iv. Only default

16. What will be the output of the following java code?


public class Main
{
t4

static int num=50;


static void testMethod()
{
ou

num=num+10;
}
public static void main(String[] args)
{
//TODOAuto-generatedmethodstub
Sh

Main m=new Main();


m.num=20;
testMethod();
System.out.println(num);
}
}

i. 60
ii. 20
iii. 50
iv. 30

Shout4Education
Shout4Education
17. You have 3 packages p1,p2 and p3. What is the expected output of compiling and
running file Tester in p3 package?

package p1;
public class Account{
protected Account(){ //line-3
System.out.println("Account");
}
}
package p2;

n
import p1.Account;
public class CurrentAcount extends Account
{

io
public CurrentAccount()
{
System.out.println("CurrentAccount");

at
}
}
package p3;
import p1.Account;
import p2.CurrentAccount; //line-3
public class Tester
{
uc
public static void main(String[] args)
Ed
{
Account c= new Account(); //line-6
CurrentAccount ca=new CurrentAccount();
}
}
t4

i. Account CurrentAccount
ii. Compilation error at line3 in package p1: Invalid access specifier ‘protected’
for ‘Account’
ou

iii. Compilation error at line3 in package p3: Cannot import ‘CurrentAccount’


class
iv. Compilation error at line6 in package p3: Constructor ‘Account’ is not visible.
Sh

Shout4Education
Shout4Education
18. Consider the following Java code. Identify the relation that exists between class
Student and class Faculty. Choose the most appropriate option.

public class Student


{
private String name;
private int rollNumber;
//getter and setter methods
}
public class Faculty

n
{
private String name;
private Student student;

io
public void setStudent(Student student)
{
this.student=student;

at
}
}

i.
ii.
iii.
iv.
Composition
Inheritance uc
There is no relationship between the given two classes
Aggregation
Ed
t4
ou
Sh

Shout4Education
Shout4Education
19. What will be the output of the following Java code?
public abstract class Employee
{
protected double salary;
public Employee(double salary)
{
this.salary=salary;
}
public abstract void computeSalary();
public double getSalary()
{
return this.salary;

n
}
}
public class ProjectManager extends Employee

io
{
private int teamSize;
public ProjectManager(double salary, int teamSize)
{

at
//Line-1
this.salary=salary;
this.teamSize=teamSize;
}

}
public void computeSalary()
{

}
uc
System.out.println(this.salary);

public class ApplicationTester


Ed
{
public static void main(String[] args)
{
Employee emp=new ProjectManager(10000.0,5);
emp.computeSalary();
}
t4

i. 10000
ii. 0
ou

iii. Null
iv. Compilation error at line-1: Implicit super constructor Employee() is
undefined. Must explicitly invoke another constructor.
Sh

Shout4Education
Shout4Education
20. What will be the output of the following Java code?
public class Test{
public static void main(String[] args) {
int i=10;
boolean b=false;
if((b==true) &&((i+=10)==20)) {
System.out.println("We are equal" +i);
}
else {
System.out.println("Not equal" +i);

n
}
}

io
}
i. Not equal ! 10
ii. We are equal !10
iii. Not equal ! 20

at
iv. We are equal ! 20

21. What will be the output of the following Java code?


public class ApplicationTester
{ uc
public static void main(String[] args)
{
Ed
for(int i=1;i<=3;i++)
{
if(i==2)
{
continue;
t4

}
for(int j=1;j<=3;j++)
{
ou

if(j==2)
{
continue;
}
System.out.println("GFT");
Sh

}
}
}
}

i. “GFT” will be displayed 9 times


ii. “GFT” will be displayed 8 times
iii. “GFT” will be displayed 6 times
iv. “GFT” will be displayed 4 times

Shout4Education
Shout4Education
22. Consider the following Java Code. When you try to execute EmployeeTester java,
the code will give a compilation error. You are required to make some modification
only to Employee.java class so that the code will execute without any compilation
error.

public class Employee


{
private int employeeId;
private String employeeName;
public Employee(String employeeName)

n
{
this.employeeName=employeeName;

io
}
public Employee(int employeeId)
{

at
this.employeeId=employeeId;
}
public void displayDetails()
{

}
}
uc
System.out.println(employeeId+" "+employName);
Ed
public class EmployeeTester
{
public static void main(String args[])
{
Employee employee1=new Employee(1001);
t4

employee1.displayDetails();
Employee employee2=new Employee();
employee2.displayDetails();
}
ou

i. Remove all the constructors from Employee.java class


ii. Remove all the parameterized constructor Employee(String) from
Sh

Employee.java class.
iii. Remove parameterized constructor Employee(int) from Employee.java class
iv. Include a default constructor in Employee.java class

23. Which of the following statements is/are TRUE?


a. Java supports multilevel inheritance
b. Super keyword is used to invoke methods from the parent class.

i. Only (a) is TRUE


ii. Only (b) is TRUE

Shout4Education
Shout4Education
iii. Both (a) and (b) are TRUE
iv. Neither (a) nor (b) are TRUE
24. Kevin is a Java programmer. He writes a Java code and compiles his code. What
type of file Kevin will get because of compiling his Java code? Choose appropriate
option.

i. .java file
ii. .exe file
iii. .class file

n
iv. .obj file

io
25. Choose from below a VALID implementation of the displayDetails method to be
inserted at line 1 to print the carmodel, average and invoke the superclass method
displayDetails. Choose appropriate answer.

at
public class vehicle {
private int vehicleId;
private String vehicleName;
public void displayDetails()
{
uc
System.out.println("Vehicle vehicleId="+vehicleId+"\n
vehicleName="+vehicleName+"]");
}
Ed
}
public class Car extends Vehicle
{
private String carModel;
private double average;
//Line1
t4

}
}

i. public void displayDetails() {System.out.println(“Car CarModel=” +


ou

carModel+”\n Average”+average+”j”); super.displayDetails();}


ii. public void displayDetails() {System.out.println(“Car CarModel=” +
carModel+”\n Average”+average+”j”); super().displayDetails();}
iii. public void displayDetails() {System.out.println(“Car CarModel=” +
Sh

carModel+”\n Average”+average+”j”); displayDetails();}


iv. public void displayDetails() {System.out.println(“Car CarModel=” +
carModel+”\n Average”+average+”j”); Vehicle.displayDetails();}

Shout4Education
Shout4Education
26. Identify how many variables/object will be in stack and heap respectively? Choose
appropriate answer.
public class DemoMemory{
public static void main(String[] args) {
int id=12345;
String name= new String("Rama");
double salary=55555.55;
}
}
i. 3 in Stack, 1 in Heap

n
ii. 2 in Stack, 2 in Heap
iii. 4 in Stack, 0 in Heap

io
iv. 0 in Stack, 4 in Heap

27. Which of the following is TRUE about the final keyword?

at
a. Final variables cannot be modified once initialized.
b. Final methods can be overridden
c. Final classes cannot be inherited

i.
ii.
iii.
Only (a)
Both (a) and (b)
Only (b)
uc
Ed
iv. Both (a) and (c)

28. With respect to Employee class identify where empNo and num are stored in the
memory? Choose appropriate answer.
t4

public class Employee{


{
int empNo;
public void setEmpNo(int num)
ou

{
this.empNo=num;
}

public int getEmpNo()


{
Sh

return this.empNo;
}
}
i. ‘empNo’ and ‘num’ both are stored in heap
ii. ‘empNo’ and ‘num’ both are stored in stack
iii. ‘empNo’ is stored in heap and ‘num’ is stored in Stack
iv. ‘empNo’ is stored in stack and ‘num’ is stored in heap

Shout4Education
Shout4Education
29. Assume that ‘Employee’ is a valid Java class. Identify the CORRECT option whch will
help you to create an array to store 3 employee objects.
i. Employee emp = new Employee[3]; for (int i=0; i<emp.length; i++){emp(i) =
new Employee();}
ii. Employee[] emp = new Employee(); for (int i=0; i<3; i++){emp(i) = new
Employee();}
iii. Employee[] emp = new Employee[3]; emp = new Employee(3)
iv. Employee[] emp = new Employee[3]; for (int i=0; i<emp.length; i++){emp(i) =
new Employee();}

n
30. Consider the Following java code (assume that Employee is a valid java class) With
respect to the below code, how many object/objects will be eligible for garbage

io
collection after line-1? Choose the most appropriate answer.

public class ApplicationTester {

at
public static void main(String[] args){
Employee emp1= new Employee();
Employee emp2= new Employee();

Employee emp4=emp1;
emp1=emp3;
emp4=null;
uc
Employee emp3= new Employee();
Ed
emp2=emp4; //line1
//some valid Java code
}
i. 1
ii. 2
t4

iii. 3
iv. 0
ou
Sh

Shout4Education
Shout4Education
31. Consider the following Java code. How many times is “Hello” printed
public class ApplicationTester
{
public static void main(String[] args)
{
int i=1;
while(i<=5)
{
if(i==2)
{
i++;

n
continue;
}
if(i==3)

io
{
break;
}

at
i++;
System.out.println("Hello");
}
}
}

i.
ii.
1 time
2 times
uc
Ed
iii. 3 times
iv. 5 times

32. Which of the following statement/statements is/are FALSE?


a. ‘package’ statement can appear any where in Java Code.
t4

b. In a Java Code, one can write only one ‘import’ statement

i. Only (a) is FALSE


ou

ii. Both (a) and (b) are FALSE


iii. Only (b) is FALSE
iv. Both (a) and (b) are TRUE
Sh

33. Which of the following statement/statements is/are FALSE?


a. Abstract method can have a body.
b. Abstract class cannot be instantiated
c. Abstract class can have a constructor

i. Both (a) and (b) are TRUE


ii. Both (b) and (c) are TRUE
iii. Both (a) and (c) are TRUE
iv. Only (b) is TRUE

Shout4Education
Shout4Education
34. Consider the following incomplete Java code. The intent of the code is to check
whether a number is odd or even. Identify the correct option which if inserted in
place of //code will meet the requirement.
public class ApplicationTester
{
static int x=0;
public static void main(String[] args)
{
int num=10;
//code

n
}
}
i. If(num%2 == 0){System.out.println(“Even”);}

io
ii. If(num%2 != 0){System.out.println(“Even”);}
iii. If(num/2 == 0){System.out.println(“Even”);}
iv. If(num/2 != 0){System.out.println(“Even”);}

at
35. Consider the following Java code. Determine the output.

interface A
{
public static final int DATA=3;
void doWork(int t);
uc
Ed
}
public class Test implements A
{
public static void main(String[] args)
{
t4

int x=5;
new Test().doWork(++x);
}
void doWork(int s)
ou

{
s=DATA+s;
System.out.println("s"+s);
}
}
Sh

i. s 14
ii. s 16
iii. s 10
iv. Compilation fails because Cannot reduce the visibility of the inherited
method from A

Shout4Education
Shout4Education
36. What will be the output of following java code?
public class Test
{
static int x=0;
public static void main(String[] args)
{
for(int x=0; x<5; x++)
{
}
System.out.println(x); //Line-8
}

n
}
i. 4
ii. 5

io
iii. 0
iv. Compilation fails at Line 8 “x cannot be resolved to a Variable”

at
37. Which code should be inserted at line-4 to get the following output? (2 4 6 8 10)
public class ArrayTester{

}
uc
public static void main(String[] args)
{
int[] evenNumbers={2,4,6,8,10};
//Code to be inserted;
System.out.println(evenNumbers[i]+"");
Ed
}
i. for (int i= 0; i<= evenNumbers.length; i++)
ii. for (int i= 0; i< evenNumbers.length-1; i++)
iii. for (int i= 0; i< evenNumbers.length; i++)
iv. for (int i= 0; i< evenNumbers.length-2; i++)
t4

38. How many string objects are created in the above code? Choose the most
appropriate answer.
ou

public class Test


{
public static void main(String[] args)
{
String s=new String("xyz");
String y="abc";
Sh

x=x+y;
}
}

i. 3
ii. 2
iii. 1
iv. 4

Shout4Education
Shout4Education
39. Predict the output of the following code.
package com.accenture.demo;
public class WhileLoopSample
{
public static void main(String[] args)
{
int number1=100;
while(number1>200); //Line-5
{
System.out.println("Welcome")
}

n
System.out.println("Hello")
}

io
i. Welcome
ii. Welcome Hello

at
iii. Hello
iv. Compilation error: “unexpected; at Line-5”

40. What is output expected for the below java code?

public class ConstructorTest{


private ConstructorTest(int w){
System.out.println(w);
uc
Ed
}
Public static ConstructorTest(){ //Line-6
System.out.println(10);
}
Public static void main(String[]args){
ConstructorTest obj=new ConstructorTest(50);
t4

}
}
ou

i. 10
ii. 10 50
iii. 50
iv. Won’t compile because of Line-6, constructor can’t be static
Sh

Shout4Education
Shout4Education
41. Consider the Java code given below. How many times will the Grandfather class
constructor be called?
class GrandFather
{
public GrandFather()
{
System.out.println("GrandFather")
}
}
class Father extends GrandFather
{

n
public Father()
{

io
System.out.println("Father")
}
class Child extends Father
{

at
public Child()
{
System.out.println("Child");
}
}

public class MainClass


{
uc
public static void main(String[] args)
Ed
{
new child();
new Father();
}
}
t4

i. 0 times
ou

ii. 1 time
iii. 2 times
iv. 3 times
Sh

Shout4Education
Shout4Education
42. Refer the below code and predict the output.
interface A{
public void method1();
}
class Demo implements A{
public void method1() {
System.out.println("1");
}
}
class ParentTest extends Demo{

n
public void method1() {
System.out.println("2");

io
}
}
public class Test extends ParentTest {

at
public static void main(String[] args){
A a=new ParentTest();
a.method1();
}
}

i. 2
ii. 1
uc
Ed
iii. 1,2
iv. 2,1
43. What will be the output of the following Java code
class Employee{
t4

private int employeeId;


private float basicPay;
public void setEmployeeId(int employeeId) {
this.employeeId=employeeId;
ou

}
public void setBasicPay(float basicPay) {
this.basicPay=basicPay;
}
}
public class EmployeeTester{
Sh

public static void main(String[] args) {


Employee emp=new Employee();
emp.setEmployeeId(1001);
emp.setBasicPay(15000.0f);
System.out.println("Employee ID:" +employeeId);
System.out.println("Basic Pay:" +emp.basicPay);
}
}

Shout4Education
Shout4Education
i. Employee ID: 1001 Basic Pay:15000.0
ii. Employee ID: 1001 Basic Pay:0.0
iii. Employee ID: 0 Basic Pay:0.0
iv. Compilation error: ‘employeeId’ and ‘basicPay’ is not visible in
‘EmployeeTester’ class
44. With respect to the java code given below, identify the number of references and
number of objects.

public class ApplicationTester {


public static void main(String[] args)

n
{
String str1= new String(“JAVA”);

io
String str2= new String(“GFT”);
String str3=str1;
Str1=str1.concat(str2);

at
Str3=str2.toUpperCase();
}

i. 4 objects and 3 references


ii.
iii.
iv.
3 objects and 4 references
2 objects and 3 references
3 objects and 3 references
uc
Ed
45. What will be output of following java code?

Trainee.java
public class Trainee{
private static int traineeCount;
t4

static{
traineeCount=1000;
}
public Trainee(){
ou

traineeCount++;
}
public static void getTraineeCount(){
System.out.println(traineeCount);
}
}
Sh

TraineeTester.java

public class TraineeTester{


public static void main(String[] args){
Trainee t1=new Trainee();
Trainee t2=new Trainee();
Trainee t3=new Trainee();
t1.getTraineeCount();
}
}

Shout4Education
Shout4Education
i. 1001
ii. 1003
iii. 1000
iv. 1002
46. What will be output of following java code?
public abstract class Employee
{
protected double salary;
public Employee(double salary) {
this.salary=salary;

n
}
public abstract void computeSalary();

io
public double getSalary() {
return this.salary;
}
}

at
public class ProjectManager extends Employee{
private int teamSize;
public ProjectManager(double salary,int teamSize){

}
//line1
this.salary=salary;
this.teamSize=teamSize;

public void computeSalary(){


uc
Ed
system.out.println(this.salary);
}
}
public class ApplicationTester{
public static void main(String[] args){
t4

Employee emp=new ProjectManager(10000.0,5);


emp.computeSalary();
}
}
ou

i. 10000
ii. 0
iii. Null
iv. Compilation error at Line-1: Implicit super constructor Employee() is
Sh

undefined. Must explicitly invoke another constructor.

Shout4Education
Shout4Education
47. Consider the following Java code and predict what will be the output?

public Interface I{
int x;//line 1
public void display();
}
public class C implements I //line2
{
public void display()
{
system.out.println("Hello");

n
}
}

io
public class TestInterface{
public static void main(String[] Args)
{
I i=new C();//line 3

at
i.display();
}
}

i.

ii.
iii.
uc
Compilation error at line-3. The method display from type Student is not
visible.
Compilation error at line-2. Cannot import class student from package x.
It will print Hello.
Ed
iv. Compilation error at line-1. No access specifier defined for method display.

48. With respect to the java code given below, Identify the relationship that exists
between class Account and class SavingsAccount.
t4

public class Account{


private double balance;
private int accountNumber;
ou

}
public SavingAccount extends Account
{
//Some valid java code
}
Sh

i. Aggregation
ii. Composition
iii. There is no relationship between class Account and class SavingaAccount
iv. Inheritance

Shout4Education
Shout4Education
49. Consider the Employee class given below. If you do not want all the subclasses of
Employee class to override the computeSalary() method, what modification will
you do for computeSalary() method?
public class Employee{
private int empNo;
private double salary;
public void computeSalary()
{//Code to compute Salary
}

n
}

i. make computeSalary() method as abstract

io
ii. make computeSalary() method as final
iii. make computeSalary() method as static

at
iv. make computeSalary() method as private

50. What is the output of the following Java code.


class Compute
{
uc
public static void main(String args[])
{
Ed
int arr1[]={1,2,3,4,5};
int arr2[]=arr1;
int sum=0;
for(int j=0;j<3;++j)
sum+=(arr1[j]*arr2[j+1]);
System.out.println(sum);
t4

}
}
i. 18
ou

ii. 20
iii. 19
iv. 21
Sh

Shout4Education
Shout4Education
51. What is the output of the following Java code.

public abstract class Employee


{
public abstract Employee()
{
System.out.println("1");
}
}
public class ProjectManager extends Employee

n
{
public ProjectManager()
{

io
System.out.println("2");
}
}

at
public class ApplicationTester
{
public static void main(String[] args)

i.
ii.
{

2
uc
Employee emp=new ProjectManager();

Compilation error: illegal modifier for the constructor in type Employee


Ed
iii. 1
iv. 1, 2

52. What is the output of the following Java code.


t4

public class Test


{
static int x = 0;
public static void main(String[]args){
ou

for (int x = 0; x<5; x++){


}
System.out.println(x); //line-8
}
}
Sh

i. 4
ii. 5
iii. 0
iv. Compilation fails at line-8 “x cannot be resolved into a variable”.

Shout4Education
Shout4Education
53. Consider the following Java code:
public class Employee{
private int empId;
String empName
}
If you want to access ‘empName’ of Employee class in all the subclasses of
employee class in the same package as well as sub classes in some other package,
which access specifier will you use? Choose the appropriate answer.

n
i. Public
ii. Private
iii. Protected

io
iv. Default

at
uc
Ed
t4
ou
Sh

Shout4Education

You might also like