Java Questions
Java Questions
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;
class B extends A {
public B() {
super(10);
}
class C extends B {
}
}
}
}
30
25
20
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--;
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;
}
}
}
System.out.println("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;
}
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;
}
Compilation error - class Employee has no default constructor
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;
System.out.println("Total amount to be paid is " +
totalAmount);
}
}
public A() {
count = 10;
}
class B extends A {
public int method1() {
return this.count = 15;
}
class C extends B {
public int method2() {
return 40;
}
}
}
}
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 < employeesSalary.length; i++) {
System.out.print(employeesSalary[i] + " ");
}
}
}
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");
input2.toLowerCase();
if (input1 == input2) {
System.out.println(" Welcome " + input1);
} else if (input1.equals(input2)) {
System.out.println(" Welcome " + input2);
} else {
System.out.println("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;
System.out.println(this.address.getZipCode());
System.out.println(customer.address.zipCode);
System.out.println(customer.address.getZipCode());
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) {
System.out.println(demo(5, 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;
}
}
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;
}
class Tester{
public static void main(String[] args) {
Employee systemEngineer = new SystemEngineer("Maria");
System.out.println(systemEngineer.getName());
}
}
Maria
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);