Java Ques
Java Ques
Java Ques
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?
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.
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
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
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
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
Shout4Education
Shout4Education
i. 3
ii. 2
iii. 1
iv. 0
12. What will be the output of the following java code?
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
}
package y;
import x.Student; //Line-2
public class TestFinalClass{
ou
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
num=num+10;
}
public static void main(String[] args)
{
//TODOAuto-generatedmethodstub
Sh
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
Shout4Education
Shout4Education
18. Consider the following Java code. Identify the relation that exists between class
Student and class Faculty. Choose the most appropriate option.
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);
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
}
for(int j=1;j<=3;j++)
{
ou
if(j==2)
{
continue;
}
System.out.println("GFT");
Sh
}
}
}
}
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.
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
Employee.java class.
iii. Remove parameterized constructor Employee(int) from Employee.java class
iv. Include a default constructor in Employee.java class
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
}
}
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
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
{
this.empNo=num;
}
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.
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
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
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”
}
}
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");
}
}
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
}
public void setBasicPay(float basicPay) {
this.basicPay=basicPay;
}
}
public class EmployeeTester{
Sh
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.
n
{
String str1= new String(“JAVA”);
io
String str2= new String(“GFT”);
String str3=str1;
Str1=str1.concat(str2);
at
Str3=str2.toUpperCase();
}
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
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;
i. 10000
ii. 0
iii. Null
iv. Compilation error at Line-1: Implicit super constructor Employee() is
Sh
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 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
}
io
ii. make computeSalary() method as final
iii. make computeSalary() method as static
at
iv. make computeSalary() method as private
}
}
i. 18
ou
ii. 20
iii. 19
iv. 21
Sh
Shout4Education
Shout4Education
51. What is the output of the following Java code.
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();
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