0% found this document useful (0 votes)
244 views14 pages

Java Code Compilation and Output Analysis

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views14 pages

Java Code Compilation and Output Analysis

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] of the following are invalid variable declarations?

double salary = 123467;


int 1value = 2147483648;
char gender = "M";
long bonus = 35000L;
boolean flag = False
String name = 'Alex'

2. Consider the code given below.


class Employee{
public int employeeId;
private double basicSalary;
private double totalSalary;
public Employee(double basicSalary) {
[Link] = basicSalary;
}

public double getBasicSalary() {


return basicSalary;
}

public double getTotalSalary() {


return totalSalary;
}

public int getEmployeeId() {


return employeeId;
}

public double calculateTotalSalary(int bonusPercent) {


//line 22
return totalSalary;
}
}

public class Tester {


public static void main(String[] args) {
Employee employee = new Employee(2150);
[Link] = 101;
[Link](8);
// line 32
}
}
You need to help the developer to display the following output by choosing the appropriate
code to be implemented in Line 21 and Line 32.
Employee Id: 101, Total salary: 2322.0
Line 21 - [Link] = [Link] * (1 + bonusPercent / 100);
Line 32 - [Link]("Employee Id: " + [Link] + ", Total salary: " +
[Link]);
Line 21 - [Link] = [Link] * (1 + (double) bonusPercent / 100);
Line 32 - [Link]("Employee Id: " + [Link]()+ ", Total salary: "
+ [Link]());
Q3 of 25outlined_flag
3. What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
int num1 = 28;
int num2 = 36;
int num3 = 0;
if (~(num2 / num1) < 0 && (num1 + num2) % 4 == 0) {
num1 = num1 + --num3;
}
if ((num2 / num1) > 1 || num3 == 0) {
num1 = num1 + num3++;
[Link](num1 + --num3);
} else {
[Link](num1 + --num3);
}
}
}

Compilation error - undefined ~ type

28

26

25

Q4 of 25outlined_flag
What will be the output of the code given below?
class A {
public int var1;
public int var2;

public A(int value) {


this.var1 = value;
}

public int method1() {


return this.var2;
}
}

class B extends A {
public B() {
super(10);
}

public int method1(int value1) {


return this.var1;
}
}

class C extends B {

public C(int value1, int value2) {


super();
this.var2 = value2;
}

public void method1(int value1, int value2) {


this.var2 = super.method1(value1);

}
}

public class Tester {


public static void main(String args[]) {
C obj= new C(5, 20);
[Link](obj.method1(5) + obj.method1());

}
}

30

25

20

Compilation error: method1() is undefined for type C


Q5 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
int num1 = -20;
int num2 = -30;
int num3 = 10;
int num4 = -40;
if (num1 + num2 >= num4) {
if (num4 < num3) {
if (num4 % num3 != 0) {
[Link](1);
} else {
[Link](2);
}
}
} else {
if (num2 / num1 > 0) {
if (num1 < num2 || num4 % num3 == 0) {
[Link](3);
} else {
[Link](4);
}
}
}
}
}
1

4
Q6 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
int num1 = 2, num2 = 20;
do {
num2 = num2 / num1;
if (num1 > num2) {
break;
}
num2--;

} while (++num1 < 5);

[Link]("num1 = " + num1 + " and num2 = " +


num2);
}
}

num1 = 4 and num2 = 0

num1 = 5 and num2 = 17

num1 = 5 and num2 = -1

num1 = 4 and num2 = -1


Q7 of 25outlined_flag
Which of the following are valid identifiers?

break$
_9Number
middle_Name
default
Class
$ dept
Q8 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
int sum = 0;
for (int i = 2; i < 8; i += 2) {
for (int j = 8; j > i; j -= 2) {
if (i >= j / 2) {
continue;
} else {
sum += i + j;

}
}
}
[Link]("Sum = " + sum);
}
}

Sum = 8

Sum = 18

Sum = 27

Sum = 36
Q9 of 25outlined_flag
How many instance variables, reference variables and objects are there in the code given
below?
class Student {
public int studentId;
public String name;
}

public class Tester {


public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = s1;
}
}

instance variables : 3, reference variables : 3, objects: 3

instance variables : 2, reference variables : 3, objects: 3

instance variables : 2, reference variables : 2, objects: 2

instance variables : 2, reference variables : 3, objects: 2


Q10 of 25outlined_flag
What will be the output of the code given below?
class Calculator {
public int num1 = 10;
public float num2 = 20;
public double sum;
public double product;
public double add(int num1, int num2) {
double sum = this.num1 + this.num2;
return sum;
}

public double multiply(int num1, int num2) {


double sum = [Link](num1, num2);
double product = [Link] * this.num1;
return (int) product;
}
}

public class Tester {


public static void main(String[] args) {
Calculator c = new Calculator();
[Link]([Link](10, 5));
}

150.0

300.0

0.0
Q11 of 25outlined_flag
What will be the output of the code given below?
class Employee {
public String name;
public char gender;
public double salary;

public Employee(String name, char gender) {


[Link] = name;
[Link] = gender;
}

public Employee(String name) {


[Link] = name;
}

public class Tester {


public static void main(String[] args) {
Employee emp1 = new Employee("Robert", 'M');
Employee emp2 = new Employee("Alex");
[Link]([Link] + ',' + [Link] + ',' +
[Link]);
}

}
Compilation error - class Employee has no default constructor

Compilation error - gender is not initialized for object emp2

Alex, , Alex

Alex, ,Robert
Q12 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
short discountPercentage = 7;
int noOfItems = 10;
float pricePerItem = 255.6f;
float taxAmount = 135.50f;
int discountedAmount = (noOfItems * (int) pricePerItem)
* (1 - discountPercentage / 100);
double totalAmount = discountedAmount + taxAmount;
[Link]("Total amount to be paid is " +
totalAmount);
}
}

Compilation error - cannot convert from float to int

Total amount to be paid is 2512.58

Total amount to be paid is 2685.5

Total amount to be paid is 2591.5


Q13 of 25outlined_flag
What will be the output of the code given below?
class A {
public int count;

public A() {
count = 10;
}

public int method1() {


int count = 20;
return [Link];
}
}

class B extends A {
public int method1() {
return [Link] = 15;
}

public int method2() {


return 20;
}
}

class C extends B {
public int method2() {
return 40;
}
}

public class Tester {


public static void main(String args[]) {
A obj1 = new A();
B obj2 = new B();
C obj3 = new C();
[Link](obj1.method1() + obj3.method1() +
obj3.method2());

}
}

75

65

70

45
Q14 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String s[]) {
int[] employeesSalary = { 1350, 2342, 6754, 1200, 1363 };
int count = 0;
for (int salary : employeesSalary) {
switch (salary % 2) {
default:
employeesSalary[count] = salary + 1;
case 0:
employeesSalary[count] = salary + 1;
count++;
case 1:
employeesSalary[count] = salary + 1;
break;
}
}
for (int i = 0; i < [Link]; i++) {
[Link](employeesSalary[i] + " ");
}
}
}

1351 2344 6755 1202 1363


1351 6755 1201 1364 1363

1351 2343 6755 1201 1364

Compilation error - default must come at the end of switch - case statements
Q15 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String args[]) {
String input1 = "warner";
String input2 = new String("WARNER");
[Link]();
if (input1 == input2) {
[Link](" Welcome " + input1);
} else if ([Link](input2)) {
[Link](" Welcome " + input2);
} else {
[Link]("Welcome");
}
}
}

Welcome

Welcome warner

Welcome WARNER

Compilation error - datatype mismatch, cannot compare String literal with String object
Q16 of 25outlined_flag
Identify the line of code to be placed in Line 27 of the code snippet given below to display
the zipCode of the customer.
class Address {
private int zipCode;

public Address(int zipCode) {


[Link]= zipCode;
}

public int getZipCode() {


return zipCode;
}
}

public class Customer {


public Address address;
public String name;

public Customer(String name, int zipCode) {


[Link] = name;
address = new Address(zipCode);
}
}

public class Tester{

public static void main(String args[]) {


Customer customer = new Customer("Sam",100001);
// Line 27
}
}

[Link]([Link]());

[Link]([Link]);

[Link]([Link]());

Compilation error - not possible to access the zipCode of Address class


Q17 of 25outlined_flag
What will be the output of the code given below?
class Validator{
public int[] studentId = { 101, 102, 103 };

public void validateStudent(int id) {


try {
for (int index = 0; index <= [Link];
index++) {
if (id == studentId[index])
[Link]("P");
}
} finally {
[Link]("Q");
}
}
}

public class Tester {


public static void main(String[] args) {
Validator validator = new Validator();
try {
[Link](101);
[Link]("R");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("S");
} finally {
[Link]("T");
}
}
}

PQST
PQRST

PQRT

PST
Q18 of 25outlined_flag
Which of the following will match the below regular expression?
String regex = "[A-Z][a-z0-9@$%&]{0,}([ ][A-Za-z]{1,})*";
"A@ "
"alex1"
"Alex2&"
"John snow"
Q19 of 25outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
[Link](demo(5, 1));
}

public static int demo(int x, int y) {


if (x == 0)
return 1;
else if (y > x)
return 0;
else
return (y + demo(x - 1, y + 1));
}
}

15

18
Q20 of 25outlined_flag
What will be the output of the code given below?
public class Calculator{
private int add(int num1, int num2){
return num1+num2;
}
}

public class Tester{


public static void main(String args[]) {
Calculator calculator = new Calculator();
[Link]([Link](1,2));
}
}
3

The code will result in a compilation error as the method add cannot be accessed outside the
class.

The code will not result in a compilation error but will not display any output.

The code will result in a compilation error as a non-static method cannot be invoked from a
static method.
Q21 of 25outlined_flag
What will be the output of the code given below?
class Employee{
private int employeeId;
private static int counter = 1000;

public Employee() {
employeeId = ++counter;
}

public int getEmployeeId() {


return employeeId;
}

public static int getCounter(){


return counter;
}
}

public class Tester {


public static void main(String[] args) {
Employee employee1= new Employee();
Employee employee2= new Employee();
Employee employee3= new Employee();
displayEmployeeDetails(employee1);
displayEmployeeDetails(employee2);
displayEmployeeDetails(employee3);
}

public static void displayEmployeeDetails(Employee employee){


[Link]([Link]() + " " +
[Link]());
}
}

1001 1003 1002 1003 1003 1003

1001 1001 1002 1002 1003 1003

1001 1002 1002 1003 1003 1004


1003 1003 1003 1003 1003 1003
Q22 of 25outlined_flag
What will be the output of the code given below?
class Parent{
public final void displayMessage() {
[Link]("displayMessage() method of Parent invoked");
}
}

class Child extends Parent{


public void displayMessage() {
[Link]("displayMessage() method of Child invoked");
}
}

public class Tester{


public static void main(String[] args) {
Parent parent = new Child();
[Link]();
}
}

displayMessage() method of Parent invoked

displayMessage() method of Child invoked

Compilation error as final method cannot be overridden

displayMessage() method of Parent invoked displayMessage() method of Child invoked


Q23 of 25outlined_flag
What will be the output of the code given below?
abstract class Employee{

private String name;

public Employee(String name) {


[Link] = name;
}

public String getName() {


return name;
}
}

class SystemEngineer extends Employee{

public SystemEngineer(String name) {


super(name);
}
}

class Tester{
public static void main(String[] args) {
Employee systemEngineer = new SystemEngineer("Maria");
[Link]([Link]());
}
}

Maria

Compilation error will occur as abstract class cannot have constructor.

Compilation error will occur as abstract class cannot be created without any abstract method.

Compilation error will occur as object cannot be created with reference of abstract class.
Q24 of 25outlined_flag
Which of the following statements is/are false?

A class can extend only one class or implement only one interface.
Methods declared in an interface are implicitly public and abstract.
Data fields declared in an interface are implicitly public, static and final.
A class can implement an interface using extends keyword.
Q25 of 25outlined_flag
Which of the given assertion methods will return true for the code given below?
Student student1 = new Student();
Student student2 = new Student();
Student student3 = student1;
The Student class is as follows.
public class Student{
private String name;
}

assertEquals(student1,student2);
assertEquals(student1,student3);
assertSame(student1,student3);
assertSame(student1,student2);

Common questions

Powered by AI

In the example, 'class Student' has two instance variables 'studentId' and 'name'. There are three reference variables: 's1', 's2', and 's3'. 's1' and 's2' each create a new 'Student' object, while 's3' references the same object as 's1', resulting in a total of two distinct objects being created .

To display the intended output, modify the code on Line 21 to 'this.totalSalary = this.basicSalary * (1 + (double) bonusPercent / 100);' and on Line 32 to 'System.out.println("Employee Id: " + employee.getEmployeeId() + ", Total salary: " + employee.getTotalSalary());' to correctly compute and access the total salary .

The loops iterate with 'i' incrementing by 2 starting from 2 while 'j' decrements by 2 starting from 8. When 'i' is half or more of 'j', the loop continues without execution. Therefore, in scenarios where 'i = 2', 'sum' accumulates values determined by adding 'i + j' only when 'i < j/2', resulting in the output 'Sum = 18' .

The '~' operator is a bitwise complement operator, which flips the bits of an integer. In the given code, the expression '~(num2 / num1)' is used. However, the operator is applied to the result of an integer division, which causes confusion if not handled properly leading to a compilation error for an invalid type use .

Given the values num1 = -20, num2 = -30, num3 = 10, and num4 = -40, the control flow first checks if num1 + num2 >= num4 (which is true), and within this block, num4 < num3 holds true. Further conditions determine the output to be '1' because num4 % num3 != 0 .

The compilation error occurs because the class C is expected to call a method 'method1()' without parameters, which is not defined or inherited. The method overloading in class B and the subsequent attempt to call such a method from class C without it actually being implemented results in the error 'method1() is undefined for type C' .

In Java, a valid identifier can contain letters (A-Z, a-z), currency characters, connecting characters (such as underscores), and digits (0-9), but it cannot start with a digit. The identifiers 'break$', '_9Number', 'middle_Name', and '$dept' are valid. However, 'default' and 'Class' are invalid as they are reserved keywords .

The code results in a compilation error because a 'final' method cannot be overridden. The method 'displayMessage()' in class 'Parent' is final, thereby preventing any subclass like 'Child' from overriding it, which violates Java's rule of immutability for 'final' methods .

Java enforces access control based on visibility modifiers such as private. In the given scenario, a private method 'add' in the 'Calculator' class is attempted to be accessed outside its defining class, leading to a compilation error as private methods are not visible outside their class .

The declaration 'int 1value = 2147483648;' is incorrect because variable names cannot start with a digit and the number 2147483648 exceeds the range of int type . 'char gender = "M";' is incorrect because single quotes must be used for character literals, not double quotes. 'boolean flag = False;' should use 'false' which is the correct boolean value in Java. Finally, 'String name = 'Alex';' should use double quotes for a String literal, not single quotes .

You might also like