Back to Dashboard

Java Programming

Complete Subject Question Bank

01 Introduction To Java#1

Using Java we can develop ___________________.

A
Desktop Application
B
Web Application
C
Both the options
D
None of these options

Correct answer: Both the options.

01 Introduction To Java#2

The main method in java should ___________.

A
take boolean[] as argument
B
be public static
C
return int
D
be private static

Correct answer: be public static.

01 Introduction To Java#3

The break statement cannot be present for _____________ construct.

A
do while
B
while
C
for
D
if

Correct answer: if.

01 Introduction To Java#4

A continue statement makes the execution jump to ______________.

A
the end of the loop
B
the first statement of the loop
C
the next iteration of the loop
D
the statement just after continue

Correct answer: the next iteration of the loop.

01 Introduction To Java#5

Identify the features of java.

A
Exception Handling
B
Multi threading
C
Direct Access to memory using pointers
D
Less security

Correct answer: Exception Handling.

01 Introduction To Java#6

a for loop, if the number of statements are greater than one, which of the following needs to be inserted at the beginning and the end of the loop?

A
Arrows<>
B
Square bracket [ ]
C
French curly braces{ }
D
Parenthesis

Correct answer: French curly braces{ }.

01 Introduction To Java#7

JRE comprises of ___________ and ___________.

A
JVM
B
tools
C
API
D
JDK

Correct answer: JVM; API.

01 Introduction To Java#8
Logic Block
1
What will be the output of the program? public class Sample { final static short a = 2; public static int b = 0; public static void main(String [] args) { for (int c=0; c < 4; c++) { switch (c) { case a: System.out.print("a "); default: System.out.print("default "); case a-1: System.out.print("a-1 "); break; case a-2: System.out.print("a-2 "); } } } }
A
a-2 a-1 a default a-1 default a-1
B
a-2 a-1 a default a-1
C
a-2 a-1 a default default
D
a default a-1

Correct answer: a-2 a-1 a default a-1 default a-1.

01 Introduction To Java#9

The ________________ statement causes the program execution to stop and JVM to shut down.

A
System.exit
B
break
C
return
D
exit

Correct answer: System.exit.

01 Introduction To Java#10

JVM is independent of OS

A
True
B
False

JVM abstracts the OS; Java bytecode runs on any JVM regardless of OS.

01 Introduction To Java#11

To compile, debug and execute a program written in java, _______________ is required.

A
JRE
B
JIT
C
JDK
D
JVM

Correct answer: JDK.

01 Introduction To Java#12

State true or false. Java is a structured programming language.

A
Java is a structured programming languag
B
TRUE
C
FALSE

Correct answer: Java is a structured programming languag.

01 Introduction To Java#13

Who is the father of Java?

A
James Gosling
B
Dennis Ritchie
C
Jim Gray
D
Donald Knuth

Correct answer: James Gosling.

01 Introduction To Java#14

What is Polymorphism?

A
ability to acquire the properties
B
blueprint for an object
C
ability to have many forms
D
hiding the properties

Correct answer: ability to have many forms.

01 Introduction To Java#15

How was Java initially named?

A
GreenTalk
B
Algol
C
The Oak
D
COBOL

Correct answer: The Oak.

01 Introduction To Java#16

Java is _____________________________.

A
Platform dependent
B
Platform independent

Correct answer: Platform independent.

01 Introduction To Java#17
Logic Block
1
What is the output of this program? 1. class Crivitch { 2. public static void main(String [] args) { 3. int x = 10; 4. 5. do { } while (x++ < y); 6. System.out.println(x); 7. } 8. } Which statement, inserted at line 4, produces the output 12?
A
int y=11;
B
int y=12;
C
int y=13;
D
int y=10;

Correct answer: int y=11;.

01 Introduction To Java#18
Logic Block
1
What will be the output of the program? Given: 10. int x = 0; 11. int y = 10; 12. do { 13. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + "," + y); What is the result?
A
6,5
B
5,5
C
5,6
D
6,6 x is assigned 0 and y, 10 initially. During each iteration x is incremented by 1 and y is decremented by 1. The iteration stops when x equals 5. At this stage y also would have reached the value 5. Hence the output 5 5.

Correct answer: 5,5.

01 Introduction To Java#19
Logic Block
1
What is the output of this program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } }
A
3
B
4
C
2
D
1 Observe the if construct. var 2 is assigned 1. 1 does not equal 5, hence else block will get execute

Correct answer: 2.

01 Introduction To Java#20
Logic Block
1
Fill in the appropriate data type for the Java switch statement: switch (____) { case value1: ... case value2: ... default: System.out.println("Hello"); }
A
byte
B
float
C
boolean
D
double

Java switch supports byte, short, char, int, their wrapper classes, enums, and String. Among the options, byte is valid.

01 Introduction To Java#21

What value is stored in i at the end of this loop? for(int i =1;i<=10;i++)

A
11
B
9
C
1
D
10 The program control will e5xit th2e fo2r loo4p on8ly when the condition specified in the for loop has faile

Correct answer: 11.

01 Introduction To Java#22

The break statement causes an exit ___________

A
from the program.
B
from the innermost loop
C
none of the options
D
from the innermost switch.

Correct answer: from the innermost loop; from the innermost switch..

01 Introduction To Java#23

Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

A
for
B
do-while
C
while

Correct answer: do-while.

01 Introduction To Java#24
Logic Block
1
French curly braces { } is a must if the for loop executes more than one statement. State true or false.
A
TRUE
B
FALSE

Correct answer: TRUE.

01 Introduction To Java#25
Logic Block
1
What will be the output of the program? for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done");
A
one two three done
B
one two three two three done
C
done
D
one two doneone two done Switch takes the values 0, 1 and 2. Case 0 has nothing to execut

Correct answer: one two three two three done.

01 Introduction To Java#26
Logic Block
1
What will be the output of the program? int i = 1, j = -1; switch (i) { case 0, 1: j = 1; /* Line 4 */ case 2: j = 2; default: j = 0; } System.out.println("j = " + j);
A
j = 0
B
j = 1
C
Compilation fails.
D
j = -1 One can not specify multiple case labels with commas, as in line 4. Hence compilation error.

Correct answer: Compilation fails..

02 Eclipse Ide#27

A project developed on one machine can be included in the current workspace by ___________ that project.

A
Exporting
B
Importing

Correct answer: Importing.

02 Eclipse Ide#28

State True or False. When typing the code in code editor, it fixes the compilation error. It also assists in how to fix that error.

A
When typing the code in code editor, it fixes the compilation error. It also assists in how to fix that error.
B
False
C
True

Correct answer: True.

02 Eclipse Ide#29

We can move an already existing project in eclipse to another location by compressing it. This we call as ________ the project.

A
Exporting
B
Importing

Correct answer: Exporting.

02 Eclipse Ide#30

Eclipse, the plugin that is needed for Java Development is ______________.

A
JDT
B
JavaPlugin
C
CDT
D
PyDev

Correct answer: JDT.

02 Eclipse Ide#31

Predict the output int a=0; if(a) System.out.println( "Hello"); else System.out.println( "Hai");

A
Need to include the braces { } for the code to work
B
Hai
C
Hello
D
Compilation Fails

Correct answer: Compilation Fails.

02 Eclipse Ide#32
Logic Block
1
What will be the output of the program? public class Sample { public static void main(String [] args) { int i = 10; do while ( i < 10 ) System.out.print("The value of i is " + i); while ( i > 10 ) ; } }
A
No output is produce
B
The value of i is 10
C
Compilation error
D
The value of i is 10 The value of i is 10

Correct answer: No output is produce.

02 Eclipse Ide#33

__________ generates the byte code for a given file with .java extension.

A
JVM
B
JDK
C
JRE

Correct answer: JDK.

02 Eclipse Ide#34

Which edition of java is used for developing web application?

A
J2ME
B
J2EE
C
J2SE
D
All these options

Correct answer: J2EE.

02 Eclipse Ide#35

State True or False For compiling a java code we have separate compilers for different OS.

A
True
B
False

Eclipse IDE creates a new workspace directory automatically if it does not exist.

02 Eclipse Ide#36

Which of the following options remain true for case constants in a switch construct?

A
If any one of the case constants has a match with the expression, then all statements after the matching case label in the switch block are executed in sequence
B
If no case matches and there is no default label, then it will result in a compilation error
C
If no case matches but there is a default label, then all statements after the matching default label in the switch block are executed in sequenc
D
If no case matches and there is no default label, then no further action is taken and the switch statement completes abnormally
E
The code with the switch construct gives a compilation error when there is a duplicate case label

Correct answer: If any one of the case constants has a match with the expression, then all statements after the matching case label in the switch block are executed in sequence.

02 Eclipse Ide#37

Who executes the byte code in java?

A
JRE
B
JDK
C
OS
D
JVM

Correct answer: JVM.

03 Class And Objects, Date Api#38

The methods of a class with the ____________ access specifier cannot be accessed in its subclass class of different package.

A
protected
B
default
C
public

Correct answer: default.

03 Class And Objects, Date Api#39
Logic Block
1
Predict the output. class X { void display(int a) { System.out.println("INT"); } void display(double d) { System.out.println("DOUBLE"); } } public class Sample { public static void main(String[] args) { new X().display(100); } }
A
INT
B
Compilation Fails
C
Ambiguity error
D
DOUBLE

Correct answer: INT.

03 Class And Objects, Date Api#40
Logic Block
1
Observe the code public class Sample { public static void main(String [] args) { int x = 6; Sample p = new Sample(); p.display(x); System.out.print(" main x = " + x); } void display(int x) { System.out.print(" display x = " + x++); } } Given in command line - java Sample - What is the result?
A
display x = 7 main x = 6
B
An exception is thrown at runtim
C
display x = 7 main x = 7
D
Compilation fails.
E
display x = 6 main x = 7 f. display x = 6 main x = 6

Correct answer: display x = 6 main x = 7 f. display x = 6 main x = 6.

03 Class And Objects, Date Api#41

Integer x1 = new Integer(120); int x2 = 120; System.out.println( x1 == x2 ); What will be the output of the above code fragment?

A
Compilation Error
B
false
C
CastException
D
true
E
Runtime Exception

Correct answer: true.

03 Class And Objects, Date Api#42
Logic Block
1
Given: public class Message { String msg; int noOfWords; public Message() { msg += " Thank you"; } public Message(int noOfWords) { this.noOfWords = noOfWords; msg = "Welcome"; Message(); } public static void main(String args[]) { Message m = new Message(5); System.out.println(m.msg); } } What will be the output ?
A
Welcome
B
Welcome Thank you
C
Welcome Thank you 5
D
Compilation fails
E
An exception is thrown at runtime f. The code runs with no output

Correct answer: Compilation fails.

03 Class And Objects, Date Api#43
Logic Block
1
Given classes defined in two different files: 1. package p1; 2. public class Test { 3. public static void display(String [] a) { /* some code */ } 4. } 1. package p2; 2. public class TestMain { 3. public static void main(String[] args) { 4. String [] names = new String[10]; 5. // insert code here 6. } 7. } Identify the statement to be written in line 5 in class TestMain to call the display method of class Test.
A
p1.Test.display(names);
B
import p1.Test.*; display(names);
C
display(names);
D
p1.display(names);
E
TestMain cannot use methods in p1

Correct answer: p1.Test.display(names);.

03 Class And Objects, Date Api#44
Logic Block
1
What is the outcome of the code? public class Item { private String description; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public static void modifyDesc(Item item,String desc) { item=new Item(); item.setDescription(desc); } public static void main(String[] args) { Item it=new Item(); it.setDescription("Gobstopper"); Item it2=new Item(); it2.setDescription("Fizzylifting"); it.modifyDesc(it,"Scrumdiddlyumptious"); System.out.println(it.getDescription()); System.out.println(it2.getDescription()); } }
A
Gobstopper Scrumdiddlyumptious
B
Scrumdiddlyumptious Fizzylifting
C
Scrumdiddlyumptious
D
Gobstopper Fizzylifting
E
Compilation fails

Correct answer: Gobstopper Fizzylifting.

03 Class And Objects, Date Api#45

Which of the following is not a Java modifier?

A
public
B
virtual
C
protected
D
private

Correct answer: virtual.

03 Class And Objects, Date Api#46
Logic Block
1
switch(a) { default: System.out.println("Welcome"); } Of which data types can the variable a be? 1. long 2. byte 3. int 4. char 5. float 6. short
A
2,3, 4 and 6
B
3, 4 and 5
C
3 and 4
D
1 and 3

Correct answer: 2,3, 4 and 6.

03 Class And Objects, Date Api#47
Logic Block
1
Predict the output. public class Test { public static void main(String args[]) { int a = 2, b = 0; for ( ; b < 20; ++b) { if (b % a == 0) continue; else if (b == 10) break; else System.out.print(b + " "); } } }
A
1 3 5 7 9 11 13 15 17 19
B
Code executes but gives no output
C
0 2 4 6 8
D
Compile-time error

The loop skips even values using continue. The break condition b == 10 is never reached because 10 is even and continue executes first, so all odd values from 1 to 19 are printed.

03 Class And Objects, Date Api#48
Logic Block
1
What will be the output of the following code? int i=20; if(i>10) { System.out.println( "The value of i is "+i); i++; if(i%2!=0) break; }
A
Compilation fails
B
Code compiles but will not execute
C
The value of i is 10
D
The value of i is 20

Correct answer: Compilation fails.

03 Class And Objects, Date Api#49

State True or False When using eclipse whichever classes are needed for the present class can be imported automatically.

A
True
B
False

Eclipse can auto-import required classes using Ctrl+Shift+O or Quick Fix.

03 Class And Objects, Date Api#50

Eclipse IDE, if we provide a workspace, it should already exist. If not, it will not open.

A
True
B
False

Eclipse IDE will create the workspace folder if it does not already exist.

03 Class And Objects, Date Api#51

___ and _____ are the access specifiers that can be applied to top level Class.

A
virtual
B
protected
C
default
D
public

Correct answer: default; public.

03 Class And Objects, Date Api#52
Logic Block
1
class Sample{ private double num = 100; private int square(int a){ return a*a; } } public class Test{ public static void main(String args[]){ Sample obj = new Sample(); System.out.println(obj.num); System.out.println(obj.square(10)); } }
A
100
B
Compile time error
C
Run time error
D
Executes but no output

Correct answer: Compile time error.

03 Class And Objects, Date Api#53
Logic Block
1
Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere. class Test { public int value; }
A
class Test { public int value; }
B
class Test { [public] int value; }

Correct answer: class Test { [public] int value; }.

03 Class And Objects, Date Api#54
Logic Block
1
Consider the below code snippet and determine the output. class Student { private int studentId; private float average; } class Test { public static void main(String a[]) { Student s=new Student(); s.studentId=123; System.out.println(s.studentId); } }
A
Compile-time error
B
Prints 123
C
0
D
Runs with no output

studentId is private; accessing it from Test directly causes a compile-time error.

03 Class And Objects, Date Api#55
Logic Block
1
The below code snippet shows an error cannot find symbol: System.out.println("BookId:"+bobj.getId()); public class Book { private int bookId; private double bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } } public class Test { public static void main(String[] args) { Book bobj=new Book(); bobj.setBookId(123); bobj.setBookPrice(500); System.out.println("BookId:"+bobj.getId()); System.out.println("BookPrice:"+bobj.getBookPrice()); } } Analyze the above code and select the correct reason for the error.
A
Method getId() does not exist; should be getBookId()
B
bookId is private
C
setBookId was not called
D
The class has no constructor

The method getId() is not defined in Book; getBookId() should be used instead.

03 Class And Objects, Date Api#56
Logic Block
1
Observe the below code. public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2
A
public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2
B
public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); [Constructor 1] new Student(54, "John"); [Constructor 2]

Correct answer: public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2.

03 Class And Objects, Date Api#57
Logic Block
1
Observe the code below. public class Student { int studentId; String name; char grade; public Student(int studentId, String name, float mark) { this.studentId = studentId; this.name=name; calculateGrade(mark); } public void calculateGrade(float mark){ if(mark>90) grade='A'; else grade='B'; } } For the code Student s = new Student(1,"Peter",95); What will be the output?
A
No output (no print statement in the code shown)
B
Compile-time error
C
Runtime error
D
Grade A assigned

The constructor runs and grade is set to A but no print statement exists in the given code.

03 Class And Objects, Date Api#58
Logic Block
1
Observe the below class. class Product{ int productId; String productName; Product { productId=0; productName= ; } Product(int id, String name) { //access Product() ---- Line 1 productId=id; productName=name; } } Identify the valid option which is used to invoke the no argument constructor, Product(), at Line 1.
A
this()
B
Product()
C
super()
D
new Product()

this() calls the no-argument constructor of the same class within a constructor.

03 Class And Objects, Date Api#59
Logic Block
1
Given the class Book and Library in two different packages : 1. package model; 2. public class Book { 3. private static void countBook { } 4. } 1. package util; 2. public class Library { 3. public static void main(String[] args) { 4. // insert code here 5. } 6. } What is required at line 4 in class Library to use the countBook method of Book class?
A
Not accessible – countBook is private
B
Book.countBook()
C
model.Book.countBook()
D
import model.Book; Book.countBook()

The countBook method is private; it cannot be accessed from the Library class in another package.

03 Class And Objects, Date Api#60

Given the class Book in packages p1 and class Main in package p2. In main to create an object of Book, which of the following are valid.

A
p1.Book bookObj=new p1.Book();
B
import p1.Book; Book bookObj=new Book();
C
import p1.*; Book bookObj=new Book();

From package p2, Book can be accessed as p1.Book directly, or via import statements.

03 Class And Objects, Date Api#61
Logic Block
1
Assume class Calculator in package p1 and CalculatorService class in package p2 as shown below. package p1; public class Calculator { __________ static void calculate(){ //some code here } } package p2; import p1.Calculator; public class CalculatorService { public void display(){ Calculator.calculate(); } } What can be the valid access specifier for the calculate method in Calculator class so that it can be accessed from CalculatorService class?
A
public
B
protected
C
private
D
default

Only public allows access from a different package without inheritance.

03 Class And Objects, Date Api#62
Logic Block
1
Choose the correct order of the Java code fragments: 1) public class Main, 2) import java.util.Scanner;, 3) {, 4) // Some code here, 5) }, 6) package test;
A
6, 2, 1, 3, 4, 5
B
1, 2, 3, 4, 5, 6
C
2, 6, 1, 3, 4, 5
D
1, 3, 2, 4, 5, 6

In Java, the package declaration comes first, followed by imports, then the class declaration and class body.

03 Class And Objects, Date Api#63
Logic Block
1
For the below code, what are the valid ways to invoke display method in the main method. public class Test { public static void display(){ } } public class Main { public static void main(String a[]){ //Invoke the display method } }
A
Test.display();
B
new Test().display();
C
display();

A static method can be called using the class name (Test.display()) or via an instance.

04 Arrays And Strings#64

Given a one-dimensional array arr, what is the correct way of getting the number of elements in arr?

A
arr.length()
B
arr.length()-1
C
arr.length
D
arr.length-1

Correct answer: arr.length().

04 Arrays And Strings#65
Logic Block
1
class ArrayTest { public static void main(String args[]) { int[] primes = new int[10]; primes[0] = "a"; System.out.println(primes[0]); } } What will be the result of compiling and executing the above code?
A
Runtime exception
B
a
C
ArrayStoreException
D
compile time error

Correct answer: compile time error.

04 Arrays And Strings#66

Partially List the correct ways of declaring an Array.

A
int studentId[10];
B
String [ ] name [ ];
C
int studentId[ ];
D
String name[]=new String(10);
E
int [ ]studentId;

Correct answer: String [ ] name [ ];; int studentId[ ];; int [ ]studentId;.

04 Arrays And Strings#67
Logic Block
1
class TestArray { public static void main(String args[]) { int arr_sample[] = new int[2]; System.out.println(arr_sample[0]); } } What will be the result of compiling and executing the above code?
A
The program does not compile because arr_sample[0] is being read before being initialize
B
The program generates a runtime exception because arr_sample[0] is being read before being initialize
C
The program compiles and prints 0 when execute
D
The program compiles and prints 1 when execute
E
The program compiles and runs but the results are not predictable because of un-initialized memory being rea

Correct answer: The program compiles and prints 0 when execute.

04 Arrays And Strings#68

String name="teknoturf"; String cname="teknoturf"; String compname=new String("teknoturf"); 1.if(name==cname) 2.if(name.equals(cname)) 3.if(name==compname) 4.if(name.equals(compname)) Identify the output.

A
equals(cname)) 3.if(name==compname) 4.if(nam
B
equals(compname)) Identify the output.
C
Line 3,4 will return5 true2. 248
D
Line 1,3,4 will return tru
E
Line 1,2,4 will return tru
F
Line 1,3 will return tru

Correct answer: Line 1,2,4 will return tru.

04 Arrays And Strings#69
Logic Block
1
What is the output of this program? class Output { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {1, 2, 3, 4, 5}; System.out.println(a1.length + " " + a2.length); } }
A
10 5
B
0 5
C
5 10
D
0 10

Correct answer: 10 5.

04 Arrays And Strings#70

StringBuilder is less eficient and slower than StringBuffer. State true or false.

A
TRUE
B
FALSE

Correct answer: TRUE.

04 Arrays And Strings#71

State True or False. Advanced for loop is better as it is less error prone as we don't need to deal with index.

A
TRUE
B
FALSE

Correct answer: TRUE.

04 Arrays And Strings#72

which of the following packages can you find String class?

A
util
B
none of the options
C
io
D
lang

Correct answer: lang.

04 Arrays And Strings#73

StringBuffer is used to create ______________.

A
a mutable String
B
an immutable String

Correct answer: a mutable String.

04 Arrays And Strings#74

String Objects are mutable. State true or false.

A
State true or fals
B
TRUE
C
FALSE

Correct answer: State true or fals.

04 Arrays And Strings#75

What does this() mean in constructor chaining concept?

A
Used for calling the current object of the same class.
B
Used for calling the parameterized constructor of the parent class.
C
Used for calling the no argument constructor of the same class.
D
Used for calling the current object of the parent class.

Correct answer: Used for calling the no argument constructor of the same class..

04 Arrays And Strings#76
Logic Block
1
package edu.ABC.model; public class Account { public static final float INTERTEST_RATE = 7.5; } Identify the correct options from the classes provided below.
A
import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTER5EST2_RAT2E; 48 } }
B
package edu.ABC.model; public class Loan { public double getInterest() { return INTEREST_RATE; } }
C
import static edu.ABC.model.Account ; public class Loan { public double getInterest() { return INTEREST_RATE; } }
D
import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } }

Correct answer: import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTER5EST2_RAT2E; 48 } }; import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } }.

04 Arrays And Strings#77

Identify which statement is true about construtors.

A
Constructor will be invoked explicitly like other methods
B
Constructor can be overloaded
C
Constructor should have same name as class name, but not case sensitive
D
Constructor of a class should not have a return type, which means the return type is void

Correct answer: Constructor can be overloaded.

04 Arrays And Strings#78

Which members of a class can be accessed by other classes is determined by the ________________

A
constructor
B
class
C
variables
D
Access specifier

Correct answer: Access specifier.

04 Arrays And Strings#79
Logic Block
1
Predict the Output of following Java Program. class Test { int x = 10; public static void main(String[] args) { System.out.println(x); } }
A
Compile Time Error
B
10
C
Runtime Exception
D
0

Correct answer: Compile Time Error.

04 Arrays And Strings#80

Identify the true statement(s). Statement 1 : When no constructor is written in a class, the compiler creates a default constructor Statement 2 : The default constructor will implicitly invoke the default / no-argument constructor of the super class Statement 3 : If a class has a parametrized constructor alone, then the compiler will create the default constructor Statement 4 : If a class has a parametrized constructor, it is mandatory to write a no-argument constructor

A
1, 2 and 3
B
2 and 3
C
1, 2 and 4
D
1 and 2

Correct answer: 1 and 2.

04 Arrays And Strings#81
Logic Block
1
Given: public class ItemTest { private final int id; public ItemTest(int id) { this.id = id; } public void updateId(int newId) { id = newId; } public static void main(String[] args) { ItemTest fa = new ItemTest(42); fa.updateId(69); System.out.println(fa.id); } } What is the result?
A
updateId(69); System.out.println(f
B
id); } } What is the result?
C
CompileTime Error
D
69
E
Runtime Error

Correct answer: CompileTime Error.

04 Arrays And Strings#82

A JavaBeans component has the following field: private boolean enabled; Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.)

A
public void setEnabled( boolean enabled ) public boolean isEnabled()
B
public boolean setEnabled( boolean enabled ) public boolean getEnabled()
C
public void setEnabled( boolean enabled ) public boolean getEnabled()
D
public void setEna5bled2( boo2lean4 ena8bled ) public void isEnabled()

When writing getters and setters, setters return type is void and getters return type is the corresponding data type. Naming convention is camelcase notation. For setters start with set followed by field name and for getters start with get followed by field name. For boolean return type, it should start with 'is' or 'are' followed by field name.

05 Regular Expression#83

The first name of a person should contain only alphabets and space. Which of the following regular expression will match the requirement?

A
Which of the following regular expression will match the requirement?
B
[a-zA-Z ]
C
[\\s]+
D
[a-zA-Z ]+
E
[\\s]

Correct answer: [a-zA-Z ].

05 Regular Expression#84

Which of the following text when matched with the regular expression [a-zA-Z&&[^aeiou]]+ will return true?

A
Good
B
must
C
My
D
cry

Correct answer: My; cry.

05 Regular Expression#85

What is the regular expression to match a whitespace character in a string?

A
True
B
False

Array index size is not negative; negative elements can be stored in an array.

05 Regular Expression#86

What is the regular expression to match a digit (0-9) in a string?

A
\d
B
[0-9]
C
\D
D
[^0-9]

\d matches any digit character (0–9) in Java regex.

05 Regular Expression#87

00means X occurs zero or more times

A
X* means X occurs zero or more times
B
X+ means X occurs one or more times
C
X? means X occurs zero or one time
D
X{n} means X occurs exactly n times

In Java regex, * quantifier means zero or more occurrences.

05 Regular Expression#88

What is the regular expression to match any email address in a string?

A
[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
B
[a-z]+@[a-z]+
C
\w+@\w+
D
[\w.-]+@[\w.-]+

A comprehensive email regex checks local part, @ symbol, domain, and TLD.

05 Regular Expression#89

Consider the below statements. Statement 1 : Matcher class interprets the pattern in a String Statement 2 : Matcher class matches the regular expression against the text provided Which of the following is true?

A
Statement 1 alone is correct.
B
Both Statement 1 and 2 are correct
C
Both Statement 1 and 2 are incorrect.
D
Statement 2 alone is correct.

Correct answer: Both Statement 1 and 2 are correct.

05 Regular Expression#90

\B means A word boundary

A
\B matches a non-word boundary
B
\b matches a word boundary
C
\B is same as \b
D
\b matches end of string

\B (uppercase) matches a non-word boundary position; \b (lowercase) matches a word boundary.

05 Regular Expression#91
Logic Block
1
Predict the output of the below code : import java.util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); } }
A
util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); } }
B
false
C
true
D
Runtime Error

Correct answer: true.

05 Regular Expression#92

What is the regular expression to match any date in the format "yyyy-mm-dd" in a string?

A
\d{4}-\d{2}-\d{2}
B
\d{2}/\d{2}/\d{4}
C
[0-9]{4}.[0-9]{2}.[0-9]{2}
D
\d{4}\d{2}\d{2}

The pattern \d{4}-\d{2}-\d{2} matches yyyy-mm-dd format dates.

05 Regular Expression#93

Observe the below code snippet String name="Sudha learns Oracle"; System.out.println(name.substring(7,12)); What is the output of the above code?

A
substring(7,12)); What is the output of the above code?
B
learn
C
learns
D
earns

Correct answer: learns.

05 Regular Expression#94

Predict the output of the below code : String emailId="john#global.com"; System.out.println(emailId.indexOf('@'));

A
indexOf('@'));
B
Any negative integer
C
-1
D
0
E
1

Correct answer: -1.

05 Regular Expression#95

What can be the parameters for the indexOf method in String class?

A
int
B
String
C
double
D
float

Correct answer: int; String.

05 Regular Expression#96

Observe the below code : String course="Java Programming"; char c=course.charAt(16); System.out.println(c); What will be the output for the above code snippet?

A
charAt(16); System.out.println(c); What will be the output for the above code snippet?
B
g
C
Compilation error
D
ArrayIndexOutOfBoundsException

Correct answer: g.

05 Regular Expression#97

Assume that the ID of an employee should start with "CBE" or "BLR" or "HYD" followed by hyphen (-) followed by 4 digits. Choose the apt regular expression that matches this text.

A
(CBE|BLR|HYD)-\d{4}
B
(CBE|BLR|HYD)\d{4}
C
[CBE|BLR|HYD]-\d{4}
D
(CBE|BLR|HYD)-[0-9]+

The regex (CBE|BLR|HYD)-\d{4} matches the required employee ID format.

05 Regular Expression#98

Which of the following matches X occurs n or more times?

A
X{n,}
B
X{n}
C
X{n,m}
D
X+

X{n,} matches X repeated n or more times in Java regex.

1. Introduction To Java#99

The ________________ statement causes the program execution to stop and JVM to shut down.

A
exit
B
break
C
return
D
System.exit

Correct answer: exit.

1. Introduction To Java#100

Using Java we can develop ___________________.

A
Desktop Application
B
Web Application
C
Both the options
D
None of these options

Correct answer: Both the options.

1. Introduction To Java#101

The break statement cannot be present for _____________ construct.

A
while
B
do while
C
for
D
if

Correct answer: if.

1. Introduction To Java#102

A continue statement makes the execution jump to ______________.

A
the end of the loop
B
the statement just after continue
C
the first statement of the loop
D
the next iteration of the loop

Correct answer: the next iteration of the loop.

1. Introduction To Java#103
Logic Block
1
What will be the output of the program? public class Sample { final static short a = 2; public static int b = 0; public static void main(String [] args) { for (int c=0; c < 4; c++) { switch (c) { case a: System.out.print("a "); default: System.out.print("default "); case a-1: System.out.print("a-1 "); break; case a-2: System.out.print("a-2 "); } } } } }
A
a-2 a-1 a default default
B
a-2 a-1 a default a-1
C
a default a-1
D
a-2 a-1 a default a-1 default a-1

Correct answer: a-2 a-1 a default a-1.

1. Introduction To Java#104

a for loop, if the number of statements are greater than one, which of the following needs to be inserted at the beginning and the end of the loop?

A
Square bracket [ ]
B
French curly braces{ }
C
Parenthesis
D
Arrows<>

Correct answer: French curly braces{ }.

1. Introduction To Java#105

Identify the features of java.

A
Multi threading
B
Direct Access to memory using pointers
C
Exception Handling
D
Less security

Correct answer: Multi threading.

1. Introduction To Java#106

JRE comprises of ___________ and ___________.

A
tools
B
JDK
C
API
D
JVM

Correct answer: API; JVM.

1. Introduction To Java#107

The main method in java should ___________.

A
take boolean[] as argument
B
return int
C
be private static
D
be public static

Correct answer: be public static.

1. Introduction To Java#108

Java is _____________________________.

A
Platform independent
B
Platform dependent

Correct answer: Platform independent.

1. Introduction To Java#109

JVM is independent of OS

A
True
B
False

JVM is platform-independent; it abstracts the underlying OS.

1. Introduction To Java#110

To compile, debug and execute a program written in java, _______________ is required.

A
JRE
B
JDK
C
JIT
D
JVM

Correct answer: JDK.

1. Introduction To Java#111

How was Java initially named?

A
The Oak
B
GreenTalk
C
COBOL
D
Algol

Correct answer: The Oak.

1. Introduction To Java#112

State true or false. Java is a structured programming language.

A
Java is a structured programming languag
B
TRUE
C
FALSE

Correct answer: Java is a structured programming languag.

1. Introduction To Java#113

Who is the father of Java?

A
James Gosling
B
Dennis Ritchie
C
Donald Knuth
D
Jim Gray

Correct answer: James Gosling.

1. Introduction To Java#114

What is Polymorphism?

A
ability to have many forms
B
hiding the properties
C
blueprint for an object
D
ability to acquire the properties

Correct answer: ability to have many forms.

1. Introduction To Java#115

The break statement causes an exit ___________

A
from the innermost switch.
B
none of the options
C
from the program.
D
from the innermost loop

Correct answer: from the innermost switch.; from the innermost loop.

1. Introduction To Java#116
Logic Block
1
What is the output of this program? 1. class Crivitch { 2. public static void main(String [] args) { 3. int x = 10; 4. 5. do { } while (x++ < y); 6. System.out.println(x); 7. } 8. } Which statement, inserted at line 4, produces the output 12?
A
int y=10;
B
int y=11;
C
int y=13;
D
int y=12;

Correct answer: int y=11;.

1. Introduction To Java#117
Logic Block
1
Fill in the appropriate data type for the Java switch statement: switch (____) { case value1: ... case value2: ... default: System.out.println("Hello"); }
A
byte
B
float
C
boolean
D
double

Java switch supports byte, short, char, int, their wrapper classes, enums, and String. Among the options, byte is valid.

1. Introduction To Java#118

What value is stored in i at the end of this loop? for(int i =1;i<=10;i++)

A
10
B
11
C
9
D
1 The program control will exit the for loop only when the condition specified in the for loop has faile

Correct answer: 11.

1. Introduction To Java#119
Logic Block
1
What is the output of this program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } }
A
1
B
2
C
4
D
3 Observe the if construct. var 2 is assigned 1. 1 does not equal 5, hence else block will get execute

Correct answer: 2.

1. Introduction To Java#120
Logic Block
1
What will happen when the following code is compiled and run in Java 1.8? int i = 1, j = -1; switch(i) switch (i) { case 0, 1: j = 1; /* Line 4 */ case 2: j = 2; default: j = 0; } System.out.println("j = " + j);
A
j = 0
B
j = -1
C
j = 1
D
Compilation fails. One can not specify multiple case labels with commas, as in line 4. Hence compilation error.

Correct answer: Compilation fails. One can not specify multiple case labels with commas, as in line 4. Hence compilation error..

1. Introduction To Java#121
Logic Block
1
What will be the output of the program? for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done");
A
done
B
one two three done
C
one two doneone two done
D
one two three two three done Switch takes the values 0, 1 and 2. Case 0 has nothing to execut

Correct answer: done.

1. Introduction To Java#122
Logic Block
1
What will be the output of the program? Given: 10. int x = 0; 11. int y = 10; 12. do { 13. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + "," + y); What is the result?
A
5,5
B
6,6
C
6,5
D
5,6 x is assigned 0 and y, 10 initially. During each iteration x is incremented by 1 and y is decremented by 1. The iteration stops when x equals 5. At this stage y also would have reached the value 5. Hence the output 5 5.

Correct answer: 5,5.

1. Introduction To Java#123
Logic Block
1
French curly braces { } is a must if the for loop executes more than one statement. State true or false.
A
TRUE
B
FALSE

Correct answer: TRUE.

1. Introduction To Java#124

Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

A
do-while
B
for
C
while

Correct answer: do-while.

2. Eclipse Ide#125

State True or False. When typing the code in code editor, it shows the compilation error. It also assists in how to fix that error.

A
When typing the code in code editor, it shows the compilation error. It also assists in how to fix that error.
B
False
C
True

Correct answer: True.

2. Eclipse Ide#126

Eclipse, the plugin that is needed for Java Development is ______________.

A
CDT
B
PyDev
C
JDT
D
JavaPlugin

Correct answer: JDT.

2. Eclipse Ide#127

A project developed on one machine can be included in the current workspace by ___________ that project.

A
Importing
B
Exporting

Correct answer: Importing.

2. Eclipse Ide#128

We can move an already existing project in eclipse to another location by compressing it. This we call as ________ the project.

A
Importing
B
Exporting

Correct answer: Exporting.

2. Eclipse Ide#129

Which of the following options remain true for case constants in a switch construct?

A
The code with the switch construct gives a compilation error when there is a duplicate case label
B
If no case matches and there is no default label, then no further action is taken and the switch statement completes abnormally
C
If any one of the case constants has a match with the expression, then all statements after the matching case label in the switch block are executed in sequence
D
If no case matches and there is no default label, then it will result in a compilation error
E
If no case matches but there is a default label, then all statements after the matching default label in the switch block are executed in sequenc

Correct answer: The code with the switch construct gives a compilation error when there is a duplicate case label.

2. Eclipse Ide#130
Logic Block
1
What will be the output of the program? public class Sample { public static void main(String[] args) { int i = 10; do while (i < 10) { System.out.print("The value of i is " + i); } while(i>10); while (i > 10); } }
A
No output is produce
B
Compilation error
C
The value of i is 10
D
The value of i is 10 The value of i is 10

Correct answer: No output is produce.

2. Eclipse Ide#131

State True or False For compiling a java code we have separate compilers for different OS.

A
True
B
False

Java uses a single platform-independent compiler (javac); it compiles to bytecode, not OS-specific code.

2. Eclipse Ide#132

__________ generates the byte code for a given file with .java extension.

A
JVM
B
JDK
C
JRE

Correct answer: JDK.

2. Eclipse Ide#133

Who executes the byte code in java?

A
OS
B
JVM
C
JDK
D
JRE

Correct answer: JVM.

2. Eclipse Ide#134

Which edition of java is used for developing web application?

A
J2ME
B
J2EE
C
J2SE
D
All these options

Correct answer: J2EE.

2. Eclipse Ide#135

Predict the output int a=0; if(a) System.out.println( "Hello"); else System.out.println( "Hai");

A
Hello
B
Compilation Fails
C
Need to include the braces { } for the code to work
D
Hai

Correct answer: Compilation Fails.

3. Class And Objects, Date Api#136

Which of the following is not a Java modifier?

A
protected
B
virtual
C
public
D
private

Correct answer: virtual.

3. Class And Objects, Date Api#137
Logic Block
1
Predict the output. class X { void display(int a) { System.out.println("INT"); } void display(double d) { System.out.println("DOUBLE"); } } public class Sample { public static void main(String[] args) { new X().display(100); } }
A
INT
B
DOUBLE
C
Ambiguity error
D
Compilation Fails

Correct answer: INT.

3. Class And Objects, Date Api#138

Integer x1 = new Integer(120); int x2 = 120; System.out.println( x1 == x2 ); What will be the output of the above code fragment?

A
false
B
CastException
C
true
D
Runtime Exception
E
Compilation Error

Correct answer: true.

3. Class And Objects, Date Api#139
Logic Block
1
Observe the code public class Sample { public static void main(String [] args) { int x = 6; Sample p = new Sample(); p.display(x); System.out.print(" main x = " + x); } voiddisplay(intx){ void display(int x) { System.out.print(" display x = " + x++); } } Given in command line - java Sample - What is the result?
A
Compilation fails.
B
display x = 6 main x = 6
C
display x = 7 main x = 7
D
display x = 6 main x = 7
E
An exception is thrown at runtim
F
f. display x = 7 main x = 6

Correct answer: display x = 6 main x = 6.

3. Class And Objects, Date Api#140
Logic Block
1
Given classes defined in two different files: 1. package p1; 2. public class Test { 3. public static void display(String [] a) { /* some code */ } 4. } 1. package p2; 2. public class TestMain { 3. public static void main(String[] args) { 4. String [] names = new String[10]; 5. // insert code here 6. } 7. } Identify the statement to be written in line 5 in class TestMain to call the display method of class Test.
A
p1.display(names);
B
p1.Test.display(names);
C
display(names);
D
TestMain cannot use methods in p1
E
import p1.Test.*; display(names);

Correct answer: p1.Test.display(names);.

3. Class And Objects, Date Api#141

The methods of a class with the ____________ access specifier cannot be accessed in its subclass class of different package.

A
default
B
public
C
protected

Correct answer: default.

3. Class And Objects, Date Api#142
Logic Block
1
What is the outcome of the code? public class Item { private String description; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public static void modifyDesc(Item item,String desc) { item=new Item(); item.setDescription(desc); } public static void main(String[] args) { Item it=new Item(); it.setDescription("Gobstopper"); Item it2=new Item(); it2.setDescription("Fizzylifting"); it.modifyDesc(it,"Scrumdiddlyumptious"); System.out.println(it.getDescription()); System.out.println(it2.getDescription()); } }
A
Gobstopper Fizzylifting
B
Scrumdiddlyumptious Fizzylifting
C
Gobstopper Scrumdiddlyumptious
D
Compilation fails
E
Scrumdiddlyumptious

Correct answer: Gobstopper Fizzylifting.

3. Class And Objects, Date Api#143
Logic Block
1
Given: public class Message { String msg; int noOfWords; public Message() { msg += " Thank you"; } public Message(int noOfWords) { this.noOfWords = noOfWords; msg = "Welcome"; Message(); } public static void main(String args[]) { Message m = new Message(5); System.out.println(m.msg); } } What will be the output ?
A
Welcome
B
Compilation fails
C
An exception is thrown at runtime
D
The code runs with no output
E
Welcome Thank you f. Welcome Thank you 5

Correct answer: Compilation fails.

3. Class And Objects, Date Api#144
Logic Block
1
Predict the output. public class Test { public static void main(String args[]) { int a = 2, b = 0; for ( ; b < 20; ++b) { if (b % a == 0) continue; else if (b == 10) break; else System.out.print(b + " "); } } }
A
1 3 5 7 9 11 13 15 17 19
B
Code executes but gives no output
C
0 2 4 6 8
D
Compile-time error

The loop skips even values using continue. The break condition b == 10 is never reached because 10 is even and continue executes first, so all odd values from 1 to 19 are printed.

3. Class And Objects, Date Api#145

Eclipse IDE, if we provide a workspace, it should already exist. If not, it will not open.

A
True
B
False

Eclipse IDE creates a new workspace automatically if the specified path does not exist.

3. Class And Objects, Date Api#146
Logic Block
1
What will be the output of the following code? int i=20; if(i>10) { System.out.println( "The value of i is "+i); i++; if(i%2!=0) break; }
A
Compilation fails
B
The value of i is 10
C
The value of i is 20
D
Code compiles but will not execute

Correct answer: Compilation fails.

3. Class And Objects, Date Api#147
Logic Block
1
switch(a) { default: System.out.println("Welcome"); } Of which data types can the variable a be? 1. long 2. byte 3. int 4. char 5. float 6. short
A
3, 4 and 5
B
2,3, 4 and 6
C
1 and 3
D
3 and 4

Correct answer: 2,3, 4 and 6.

3. Class And Objects, Date Api#148

State True or False When using eclipse whichever classes are needed for the present class can be imported automatically.

A
True
B
False

Eclipse can automatically add missing imports via Quick Fix or Organize Imports.

3. Class And Objects, Date Api#149

___ and _____ are the access specifiers that can be applied to top level Class.

A
virtual
B
public
C
default
D
protected

Correct answer: public; default.

3. Class And Objects, Date Api#150
Logic Block
1
class Sample{ private double num = 100; private int square(int a){ return a*a; } } public class Test{ public static void main(String args[]){ Sample obj = new Sample(); System.out.println(obj.num); System.out.println(obj.square(10)); } }
A
Executes but no output
B
Compile time error
C
100
D
Run time error

Correct answer: Compile time error.

3. Class And Objects, Date Api#151
Logic Block
1
Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere. class Test { public int value; }
A
class Test { public int value; }
B
class Test { [public] int value; }

Correct answer: class Test { [public] int value; }.

3. Class And Objects, Date Api#152
Logic Block
1
Consider the below code snippet and determine the output. class Student { private int studentId; private float average; } class Test { public static void main(String a[]) { Student s=new Student(); s.studentId=123; System.out.println(s.studentId); } }
A
Compile-time error
B
Prints 123
C
0
D
Runs with no output

studentId is private; accessing it from outside the class causes a compile-time error.

3. Class And Objects, Date Api#153
Logic Block
1
The below code snippet shows an error cannot find symbol: System.out.println("BookId:"+bobj.getId()); public class Book { private int bookId; private double bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } } public class Test { public static void main(String[] args) { Book bobj=new Book(); bobj.setBookId(123); bobj.setBookPrice(500); (" " ()) System.out.println("BookId:"+bobj.getId()); System.out.println("BookPrice:"+bobj.getBookPrice()); } } Analyze the above code and select the correct reason for the error.
A
Method getId() does not exist in Book; getBookId() should be used
B
bookId is private
C
Wrong method name called
D
Constructor missing

The error is that getId() is not defined; the correct method is getBookId().

3. Class And Objects, Date Api#154
Logic Block
1
Observe the below code. public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2
A
public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2
B
public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); [Constructor 1] new Student(54, "John"); [Constructor 2]

Correct answer: public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2.

3. Class And Objects, Date Api#155
Logic Block
1
Observe the code below. public class Student { int studentId; String name; char grade; public Student(int studentId, String name, float mark) { this.studentId = studentId; this.name=name; calculateGrade(mark); } public void calculateGrade(float mark){ if(mark>90) grade='A'; else grade='B'; } } For the code Student s = new Student(1,"Peter",95); What will be the output?
A
No output (no print statement in given code)
B
Compile-time error
C
Grade B
D
Grade A

The code runs without error but includes no print statement, so no output is produced.

3. Class And Objects, Date Api#156
Logic Block
1
Observe the below class. class Product{ int productId; String productName; Product { productId=0; productName= ; } Product(int id, String name) { //access Product() ---- Line 1 productId=id; productName=name; } } Identify the valid option which is used to invoke the no argument constructor, Product(), at Line 1.
A
this()
B
Product()
C
super()
D
new Product()

this() is used within a constructor to call another constructor of the same class.

3. Class And Objects, Date Api#157

Given the class Book in packages p1 and class Main in package p2. In main to create an object of Book, which of the following are valid.

A
p1.Book bookObj=new p1.Book();
B
import p1.Book; Book bookObj=new Book();
C
import p1.*; Book bookObj=new Book();

From package p2, Book can be accessed as p1.Book directly, or via import statements.

3. Class And Objects, Date Api#158
Logic Block
1
Given the class Book and Library in two different packages : 1. package model; 2. public class Book { 3. private static void countBook { } 4. } 1. package util; 2. public class Library { 3. public static void main(String[] args) { 4. // insert code here 5. } 6. } What is required at line 4 in class Library to use the countBook method of Book class?
A
Not accessible – countBook is private
B
Book.countBook()
C
model.Book.countBook()
D
import model.Book; Book.countBook()

countBook is private; it cannot be accessed from Library in a different package.

3. Class And Objects, Date Api#159
Logic Block
1
Choose the correct order of the Java code fragments: 1) public class Main, 2) import java.util.Scanner;, 3) {, 4) // Some code here, 5) }, 6) package test;
A
6, 2, 1, 3, 4, 5
B
1, 2, 3, 4, 5, 6
C
2, 6, 1, 3, 4, 5
D
1, 3, 2, 4, 5, 6

In Java, the package declaration comes first, followed by imports, then the class declaration and class body.

3. Class And Objects, Date Api#160
Logic Block
1
Assume class Calculator in package p1 and CalculatorService class in package p2 as shown below. package p1; public class Calculator { __________ static void calculate(){ //some code here } } package p2; import p1.Calculator; public class CalculatorService { public void display(){ Calculator.calculate(); } } What can be the valid access specifier for the calculate method in Calculator class so that it can be accessed from CalculatorService class?
A
public
B
protected
C
private
D
default

Only public allows cross-package static method access from CalculatorService.

3. Class And Objects, Date Api#161
Logic Block
1
For the below code, what are the valid ways to invoke display method in the main method. public class Test { public static void display(){ } } public class Main { public static void main(String a[]){ //Invoke the display method } }
A
Test.display();
B
new Test().display();
C
display();

Static methods can be invoked via class name or via an instance reference.

4. Arrays And Strings#162

List the correct ways of declaring an Array.

A
int [ ]studentId;
B
String [ ] name [ ];
C
int studentId[ ];
D
int studentId[10];
E
String name[]=new String(10);

Correct answer: int [ ]studentId;; String [ ] name [ ];; int studentId[ ];.

4. Arrays And Strings#163
Logic Block
1
What is the output of this program? class Output { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {1, 2, 3, 4, 5}; System.out.println(a1.length + " " + a2.length); } }
A
0 5
B
5 10
C
0 10
D
10 5

Correct answer: 0 5.

4. Arrays And Strings#164

StringBuffer is used to create ______________.

A
an immutable String
B
a mutable String

Correct answer: a mutable String.

4. Arrays And Strings#165

Given a one-dimensional array arr, what is the correct way of getting the number of elements in arr?

A
arr.length-1
B
arr.length()-1
C
arr.length
D
arr.length()

Correct answer: arr.length-1.

4. Arrays And Strings#166
Logic Block
1
class ArrayTest { public static void main(String args[]) { int[] primes = new int[10]; primes[0] = "a"; System.out.println(primes[0]); } } What will be the result of compiling and executing the above code?
A
a
B
compile time error
C
Runtime exception
D
ArrayStoreException

Correct answer: compile time error.

4. Arrays And Strings#167

State True or False. Advanced for loop is better as it is less error prone as we don't need to deal with index.

A
TRUE
B
FALSE

Correct answer: TRUE.

4. Arrays And Strings#168

String name="teknoturf"; String cname="teknoturf"; String compname=new String("teknoturf"); 1.if(name==cname) 2.if(name.equals(cname)) 3.if(name==compname) 4.if(name.equals(compname)) Identify the output.

A
equals(cname)) 3.if(name==compname) 4.if(nam
B
equals(compname)) Identify the output.
C
Line 1,2,4 will return tru
D
Line 3,4 will return tru
E
Line 1,3 will return tru
F
Line 1,3,4 will return tru

Correct answer: Line 1,2,4 will return tru.

4. Arrays And Strings#169

String Objects are mutable. State true or false.

A
State true or fals
B
TRUE
C
FALSE

Correct answer: State true or fals.

4. Arrays And Strings#170
Logic Block
1
class TestArray { public static void main(String args[]) { int arr_sample[] = new int[2]; System.out.println(arr_sample[0]); } } What will be the result of compiling and executing the above code?
A
The program compiles and prints 1 when execute
B
The program compiles and runs but the results are not predictable because of un-initialized memory being rea
C
The program generates a runtime exception because arr_sample[0] is being read before being initialize
D
The program compiles and prints 0 when execute
E
The program does not compile because arr_sample[0] is being read before being initialize

Correct answer: The program compiles and prints 1 when execute.

4. Arrays And Strings#171

which of the following packages can you find String class?

A
lang
B
none of the options
C
io
D
util

Correct answer: lang.

4. Arrays And Strings#172

StringBuilder is less efficient and slower than StringBuffer. State true or false.

A
TRUE
B
FALSE

Correct answer: TRUE.

4. Arrays And Strings#173
Logic Block
1
Given: public class ItemTest { private final int id; public ItemTest(int id) { this.id = id; } public void updateId(int newId) { id = newId; } public static void main(String[] args) { ItemTest fa = new ItemTest(42); fa.updateId(69); System.out.println(fa.id); } } What is the result?
A
updateId(69); System.out.println(f
B
id); } } What is the result?
C
Runtime Error
D
69
E
CompileTime Error

Correct answer: CompileTime Error.

4. Arrays And Strings#174
Logic Block
1
Predict the Output of following Java Program. class Test { int x = 10; public static void main(String[] args) { System.out.println(x); } }
A
0
B
10
C
Runtime Exception
D
Compile Time Error

Correct answer: Compile Time Error.

4. Arrays And Strings#175

Identify the true statement(s). Statement 1 : When no constructor is written in a class, the compiler creates a default constructor Statement 2 : The default constructor will implicitly invoke the default / no-argument constructor of the super class Statement 3 : If a class has a parametrized constructor alone, then the compiler will create the default constructor Statement 4 : If a class has a parametrized constructor, it is mandatory to write a no-argument constructor

A
2 and 3
B
1 and 2
C
1, 2 and 4
D
1, 2 and 3

Correct answer: 1 and 2.

4. Arrays And Strings#176

A JavaBeans component has the following field: private boolean enabled; Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.)

A
public void setEnabled( boolean enabled ) public boolean getEnabled()
B
public void setEnabled( boolean enabled ) public boolean isEnabled()
C
public void setEnabled( boolean enabled ) public void isEnabled()
D
publicbooleansetEnabled(booleanenabled)
E
public boolean setEnabled( boolean enabled ) public boolean getEnabled()

When writing getters and setters, setters return type is void and getters return type is the corresponding data type. Naming convention is camelcase notation. For setters start with set followed by field name and for getters start with get followed by field name. For boolean return type, it should start with 'is' or 'are' followed by field name.

4. Arrays And Strings#177
Logic Block
1
package edu.ABC.model; public class Account { public static final float INTERTEST_RATE = 7.5; } Identify the correct options from the classes provided below.
A
import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } }
B
import static edu.ABC.model.Account ; public class Loan { public double getInterest() { return INTEREST_RATE; } }
C
package edu.ABC.model; public class Loan { public double getInterest() { return INTEREST_RATE; } }
D
import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTEREST_RATE; } }

Correct answer: import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } }; import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTEREST_RATE; } }.

4. Arrays And Strings#178

Which members of a class can be accessed by other classes is determined by the ________________

A
Access specifier
B
class
C
constructor
D
variables

Correct answer: Access specifier.

4. Arrays And Strings#179

Identify which statement is true about construtors.

A
Constructor can be overloaded
B
Constructor of a class should not have a return type, which means the return type is void
C
Constructor should have same name as class name, but not case sensitive
D
Constructor will be invoked explicitly like other methods

Correct answer: Constructor can be overloaded.

4. Arrays And Strings#180

What does this() mean in constructor chaining concept?

A
Used for calling the no argument constructor of the same class.
B
Used for calling the current object of the parent class.
C
Used for calling the parameterized constructor of the parent class.
D
Used for calling the current object of the same class.

Correct answer: Used for calling the no argument constructor of the same class..

4. Arrays And Strings#181
Logic Block
1
class Output { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {1, 2, 3, 4, 5}; System.out.println(a1.length + " " + a2.length); } }
A
10 5
B
5 10
C
0 10
D
0 5 Array a1 is created so as to contain 10 integer elements. Hence, the length is 10. Array a2 is initialized with 5 values. . Hence, the length is 5. y g

Correct answer: 10 5.

4. Arrays And Strings#182

_______________ is used to allocate memory to array variable in Java

A
malloc
B
new
C
alloc
D
calloc

Correct answer: new.

4. Arrays And Strings#183
Logic Block
1
Predict the output p class String_demo { public static void main(String args[]) { int ascii[] = { 65, 66, 67, 68}; String s = new String(ascii, 1, 3); System.out.println(s); } }
A
ABCD
B
ABC
C
CDA
D
BCD An integer array is initialized with values 65, 66, 67 and 68. Its reference is "ascii". A new string object is initialized with this reference such that the elements from index 1 through 3 alone gets copied as "characters". This object is referred by "s". Printing this object will output BCD which are the char-equivalents of 66, 67 and 68.

Correct answer: ABCD.

4. Arrays And Strings#184
Logic Block
1
Determine the output: class Evaluate { public static void main(String args[]) { int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = 6; n = arr[arr[n] / 2]; System.out.println(arr[n] / 2); } }
A
0
B
1
C
3
D
6 arr is an integer array that is initialized with 10 values. When n is initialized with value 6, n = arr[arr[n] / 2] evaluates to n = 3. Now, printing arr[n] / 2 will output 1.

Correct answer: 1.

4. Arrays And Strings#185
Logic Block
1
Predict the output class String_demo { public static void main(String args[]) { char chars[] = {'a', 'b', 'c'}; String s = new String(chars); System.out.println(s); } }
A
a
B
b
C
abc
D
c A character array is initialized with 'a', 'b' and 'c' and the array reference is chars. Printing this reference will output ab
E
A "new" string object is initialized with this reference and this object is referred by "s". Printing this reference will output ab

Correct answer: a.

4. Arrays And Strings#186
Logic Block
1
Determine the output: (MCQ) public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0]is"+x[0]); Sys e .ou .p ( [ ] s [ ]); } }
A
The program has a runtime error because the array elements are not initialize
B
The program has a runtime error because the array element x[0] is not define
C
The program has a compile error because the size of the array wasn't specified when declaring the array.
D
The program runs fine and displays x[0] is 0. The "new" keyword allows memory for storing integer elements in an array to be created in the "heap" and the memory is initialized with "default of integer" which is 0.

Correct answer: The program runs fine and displays x[0] is 0. The "new" keyword allows memory for storing integer elements in an array to be created in the "heap" and the memory is initialized with "default of integer" which is 0..

4. Arrays And Strings#187
Logic Block
1
Given: 1. public class MyLogger { 2. private StringBuilder logger = new StringBuuilder(); 3. public void log(String message, String user) { 4. logger.append(message); 5. logger.append(user); 6. } 7. } The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system. How must this code be changed to be thread-safe?
A
Replace StringBuilder with just a String object and use the string concatenation (+=) within the log metho
B
Synchronize the log method
C
Replace StringBuilder with StringBuffer
D
No change is necessary, the current MyLogger code is already thread-saf

Correct answer: Replace StringBuilder with StringBuffer.

4. Arrays And Strings#188

________________ is used to find string length.

A
length()
B
len
C
size()
D
length

Correct answer: length().

4. Arrays And Strings#189
Logic Block
1
Determine the output: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for(int i = 0; i < x.length; i++) System.out.print(y[i] + " "); } }
A
0 0
B
1 2

Correct answer: 1 2.

4. Arrays And Strings#190

What is special about string objects as compared to objects of other derived types?

A
You can create string objects without or without using new operator
B
Java provides string constant pool to store the string objects
C
You can concatenate two string objects using +

Correct answer: You can create string objects without or without using new operator; Java provides string constant pool to store the string objects; You can concatenate two string objects using +.

4. Arrays And Strings#191

__________________ is the string contained in s after following lines of code? StringBuffer s = new StringBuffer(Hello); s.deleteCharAt(0);

A
llo
B
hell
C
Hel
D
ello

Correct answer: llo.

4. Arrays And Strings#192
Logic Block
1
What will be the content of array variable table after executing the following code? public class Trial { public static void main(String[] args) { int []table[]=new int[5][5]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(j == i) { table[i][j] = 1; System.out.print(table[i][j]); } else { table[i][j] = 0; System.out.print(table[i][j]); } } System.out.println("\n"); } } }
A
100 010 001
B
Compilation error
C
111 111 111
D
000 000 000

The two-dimensional int array declaration is valid. The nested loop sets diagonal cells to 1 and other visited cells to 0, printing a 3x3 identity pattern.

4. Arrays And Strings#193

What will s2 contain after following lines of code? String s1 = one; String s2 = s1.concat(two);

A
one
B
onetwo
C
twoone
D
two The string "two" referred by s2 is "concatenated to" the string "one" referred by s1.

Correct answer: one.

4. Arrays And Strings#194
Logic Block
1
Determine the output class array_output { public static void main(String args[]) { char array_variable [] = new char[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = 'i'; System.out.print(array_variable[i] + ""); } } }
A
i j k l m n o p q r
B
i i i i i i i i i i
C
Hence the output "iiiiiiiiii".

Correct answer: i i i i i i i i i i.

4. Arrays And Strings#195

+ operator can be used to concatenate two or more String objects in java. State true or false.

A
State true or fals
B
TRUE
C
FALSE

Correct answer: State true or fals.

4. Arrays And Strings#196

Column size is mandatory to create an array in java. State true or false

A
TRUE
B
FALSE

Correct answer: TRUE.

4. Arrays And Strings#197
Logic Block
1
Determine the output public class Trial { public static void main(String[] args) { int arr[4]={}; System.out.print(arr[0]); } }
A
Compile time error
B
Garbage error
C
0
D
Runtime error int arr[4] is syntactically wrong

Correct answer: Compile time error.

4. Arrays And Strings#198
Logic Block
1
Determine the output: public class A { public static void main(String argv[]) { int ary[]=new int[]{1,2,3}; System.out.println(ary[1]); } }
A
2
B
1
C
Compilation Error:incorrect syntax The array ary is initialized with 3 elements and the element at the first index is 2.

Correct answer: 2.

5. Regular Expression#199

Which of the following text when matched with the regular expression [a-zA-Z&&[^aeiou]]+ will return true?

A
must
B
My
C
cry
D
Good

Correct answer: My; cry.

5. Regular Expression#200

What is the regular expression to match any date in the format "yyyy-mm-dd" in a string?

A
True
B
False

The regex \d{4}-\d{2}-\d{2} matches dates in yyyy-mm-dd format.

5. Regular Expression#201

a regular expression, the pattern 00 means, 0 occurs exactly two times.

A
True
B
False

In regex "00" means the pattern "0" appears exactly two times.

5. Regular Expression#202

What is the regular expression to match a whitespace character in a string?

A
True
B
False

\s matches any whitespace character.

5. Regular Expression#203

The first name of a person should contain only alphabets and space. Which of the following regular expression will match the requirement?

A
Which of the following regular expression will match the requirement?
B
[a-zA-Z ]+
C
[a-zA-Z ]
D
[\\s]
E
[\\s]+

Correct answer: [a-zA-Z ]+.

5. Regular Expression#204

What is the regular expression to match any email address in a string?

A
True
B
False

An email regex like [a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+ matches email addresses.

5. Regular Expression#205

Consider the below statements. Statement 1 : Matcher class interprets the pattern in a String Statement2:Matcherclassmatchestheregularexpression againstthetextprovided Statement 2 : Matcher class matches the regular expression against the text provided Which of the following is true?

A
Statement 1 alone is correct.
B
Both Statement 1 and 2 are incorrect.
C
Statement 2 alone is correct.
D
Both Statement 1 and 2 are correct

Correct answer: Both Statement 1 and 2 are correct.

5. Regular Expression#206

What is the regular expression to match a digit (0-9) in a string?

A
True
B
False

\B matches a non-word boundary position, not a word boundary (that is \b).

5. Regular Expression#207
Logic Block
1
Predict the output of the below code : import java.util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); } }
A
util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); } }
B
true
C
false
D
compile time error
E
Runtime Error

Correct answer: true.

5. Regular Expression#208

\B means A word boundary

A
True
B
False

\B in regex means NON-word boundary; \b (lowercase) means word boundary.

5. Regular Expression#209

Observe the below code snippet String name="Sudha learns Oracle"; System.out.println(name.substring(7,12)); What is the output of the above code?

A
substring(7,12)); What is the output of the above code?
B
learn
C
learns
D
earns

Correct answer: learns.

5. Regular Expression#210

Predict the output of the below code : String emailId="john#global.com"; System.out.println(emailId.indexOf('@'));

A
indexOf('@'));
B
Any negative integer
C
-1
D
0
E
1

Correct answer: -1.

5. Regular Expression#211

What can be the parameters for the indexOf method in String class?

A
String
B
int
C
float
D
double

Correct answer: String; int.

5. Regular Expression#212

Observe the below code : String course="Java Programming"; char c=course.charAt(16); System.out.println(c); What will be the output for the above code snippet?

A
charAt(16); System.out.println(c); What will be the output for the above code snippet?
B
StringIndexOutOfBoundsException
C
ArrayIndexOutOfBoundsException
D
Compilation error
E
g

Correct answer: StringIndexOutOfBoundsException.

5. Regular Expression#213

Assume that the ID of an employee should start with "CBE" or "BLR" or "HYD" followed by hyphen (-) followed by 4 digits. Choose the apt regular expression that matches this text.

A
True
B
False

The regex (CBE|BLR|HYD)-\d{4} matches IDs like CBE-1234.

5. Regular Expression#214

Which of the following matches X occurs n or more times?

A
X{n,}
B
X{n}
C
X{n,m}
D
X+

X{n,} matches X repeated n or more times.

Key Topics to Study

Based on our question bank analysis, master these concepts to score high in Java Programming.

JavaConstructorStringArraySwitchPackageEclipseCompilation
Preparation Tip

"Focus on understanding the logic behind pseudocode loops and selection statements, as they form the bulk of technical assessments."