Core Java SampleQA
Core Java SampleQA
Core Java SampleQA
switch(){
case Expr_1://statement(s)
break;
case Expr_2://statement(s)
break;
.
4 Which is the correct syntax of switch construct? .
.
case Expr_N://statement(s)
break;
default://statement(s);
}
int i=10,j=30;
9 while(i++<--j) No output
{
}
System.out.println(i+" "+j);
Predict the correct statement of the following code snippet:
Fill in the gap: "When two or more threads simultaneously access the
12 same variable and atleast one thread tries to write a value to the Lock Starvation
variable, it is called the _________."
Which of the following statements are true with respect to advantages
of multithreading?
Statement A: Maximizing the use of system resources by using threads,
13 which share the more than one address spaces and belong to the Statement A
multiple process.
Statement B: Simplifies the structure of complex application, such as
multimedia applications.
15 I. Allow a thread to wait until the thread, on which it is called, A-II, B-I, C-IV, D-III
terminates.
II. Is used to determined if the current thread has been interrupted by
another thread.
III. Is used to interrupt the execution of a thread.
IV. Is used to check the existence of a thread.
int x = 0, y = 0 , z = 0 ;
17 x = (++x + y-- ) * z++; -1
What will be the value of "x" after execution ?
18 In java, _________ can only test for equality, where as _________ can switch, if
evaluate any type of the Boolean expression.
What will be the output after the following program is compiled and
executed?
public class Test{
public static void main(String args[]){
int x = 10;
x = myMethod(x--); The will compile successfully
21 System.out.print(x); and display 9 as output.
}
class ClassicJumble{
public void show(){
System.out.println(choice);
}
Which is the correct code snippet to create a class ClassicJumble, public void static
24 contains the show() that displays the value of the variable, choice, the main(String[] args){
show() method is called by object, obj1. ClassicJumble obj1=new
ClassicJumble();
obj1.show();
}
}
for( ; ; ){
25 Which one is not an example of infinite loop? //statement(s)
}
void start()
{
long [] a1 = {3,4,5};
33 15 15
long [] a2 = fixArray(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
void start()
{
34 String s1 = "hello"; hello world
String s2 = fixString(s1);
System.out.println(s1 + " " + s2);
}
for(int i=0;i<3;i++)
{
System.out.print(rand.ints(1,5,11).findFirst().getAsInt());
}
}
}
What will be output of following program
package javaq.java8.*;
import java.time.Clock;
36 Compilation Error
public class DateTimeTest
{
public static void main(String[] arg)
{
final Clock clock=Clock.systemUTC();
System.out.println(clock.instant());
System.out.println(clock.millis());
}
}
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
37 public class ZonedDateTimeTest Compilation Error
{
public static void main(String arg[])
{
final Clock clock=Clock.systemUTC();
final ZonedDateTime zonedDatetime=ZonedDateTime.now();
final ZonedDateTime
zonedDatetimeFromClock=ZonedDateTime.now(clock);
final ZonedDateTime
zonedDatetimeFromZone=ZonedDateTime.now(ZoneId.of("America"));
System.out.println(zonedDatetime);
System.out.println(zonedDatetimeFromClock);
System.out.println(zonedDatetimeFromZone);
}
}
What will be the output of the following code.
package javaqs.question;
package javaqas.que;
str.chars().forEach(ch->System.out.println(ch));
}
}
package javaqs.que;
import java.util.Optional;
overloadedMethod(i);
}
}
overloadedMethod(i);
}
}
Why final and abstract can not be used at a time? Select the correct
answer(s) from the following:
44 Only 1 and 2
1. “final” keyword is used to denote that a class or method does not
need further improvements.
2. “abstract” keyword is used to denote that a class or method needs
further improvements.
3. A final class or method can not be modified further where as
abstract class or method must be modified further.
Consider the following code snippet of Implementation of Comparator
interface using anonymous inner class:
45 Comparator<Student> idComparator = new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getID()-s2.getID();
}
}; Comparator<Student>
idComparator = (Student s1,
Select the equivalent code of the above in java 8 using Lambda Student s2) => s1.getID()-
expression. s2.getID();
Predict the correct output of the preceding code snippet. Compile time error
47
Select the correct statements regarding Java ENUM from the following
list:
49
1. it is mandatory to declare enum constants with UPPERCASE letters.
2. Enum types like classes can have fields, constructors and methods
along with enum constants.
3. Enum constructors are public by default.
4. Enum constants are created only once for the whole execution. All
enum constants are created when you refer any enum constant first
time in your code. Only 1 and 2
Consider the following code snippet:
enum Enums
50 {
int i;
A, B, C;
} Compile time error, enum
constants must be declared
Predict the correct statement of the preceding code snippet. first.
Option2 Option3 Option4 Answer
switch(expression){
case Expr_1://statement(s) switch{
switch(expression){ break;
case Expr_2://statement(s) case Expr_1://statement(s)
case Expr_1://statement(s) break;
case Expr_2://statement(s) break; case Expr_2://statement(s)
. . break;
. . C
.
. .
case Expr_N://statement(s) case Expr_N://statement(s) ..
default://statement(s); break; case Expr_N://statement(s)
} default://statement(s); }
}
0
1 0
0 2 1
1 3 2
2 4 3
3 B
5 4
4 6 5
5 7 6
6 8 7
9
Calculating total number of Calculating total number of Calculating total number of A
rows elements indexes
A-II, B-III, C-IV, D-I A-IV, B-I, C-II, D-III A-III, B-IV, C-I, D-II C
29 31 15 15 30 30 D
The array variable, i, is used The array variable, j, is used to
The array variable, i, is used to to store the row data and the store the row data and the
store the row data and the variable, j, is used to store
variable, j, is used to store the i, is used to store the B
the column data of each jth variable,
column data of each ith row. column data of each ith row.
row.
A-I, B-II, C-III, D-IV A-II, B-III, C-I, D-IV A-IV, B-III, C-II, D-I C
In main() method
i=0
,ThreadName=Thread-0
In main() method i=1
i=0 ,ThreadName=Thread-0 ,ThreadName=Thread-0
i=1 ,ThreadName=Thread-0 i=2
i=0 ,ThreadName=Thread-1 ,ThreadName=Thread-0 None C
i=2 ,ThreadName=Thread-0 i=0
i=1 ,ThreadName=Thread-1 ,ThreadName=Thread-1
i=2 ,ThreadName=Thread-1 i=1
end main() method ,ThreadName=Thread-1
i=2
,ThreadName=Thread-1
end main() method
class ClassicJumble{
class ClassicJumble{
class ClassicJumble{ int choice=20;
int choice=20;
int choice=20; public void show(){
public void show(){
public void show(){ System.out.println(choice);
System.out.println(choice);
System.out.println(choice); }
}
} public void static
public void static D
public void static main(String[] main(String[] args){
main(String[] args){
args){ ClassicJumble obj1=new
ClassicJumble obj1;
obj1.show(); ClassicJumble();
obj1.show();
} obj1.show();
}
} }
} }
for(int i=0; i<10;){ for(int i=0; i<10;){ for(int i=0; i<10; i++){
System.out.println(++i); System.out.println(i); System.out.println(--i); B
} } }
An exception is thrown at
args[2] = 3 args[2] = null D
runtime.
static void add(int... args, int y){} static void add(int args...){} static void add(int...args){} D
1 2 3B
error at line 3 (static Compile error at line 4 (invalid
44 Compile argument type for method C
methods can't invoke this) main )
Compile the code but will not Code will execute and show
execute predicted output Exception 1
Object Oriented
Programming
Using Java
Object Oriented
Programming
Using Java
Object Oriented
Programming
Using Java
Basic
Programming
Using Java
Object Oriented
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Object Oriented
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Object Oriented
Programming
Using Java
Object Oriented
Programming
Using Java
Object Oriented
Programming
Using Java
Object Oriented
Programming
Using Java
Object Oriented
Programming
Using Java
Basic
Programming
Using Java
Basic
Programming
Using Java
Wrapper Classes
Wrapper Classes
Exception
handling
Java Datatype
Java Datatype
Q No Question
[Hints:
s1 = "JavaJ2eeStrutsHibernate"
s2 = "StrutsHibernateJavaJ2ee"
If s1 = “JavaJ2eeStrutsHibernate” then s3 =
“JavaJ2eeStrutsHibernateJavaJ2eeStrutsHibernate”.
Java does support copy constructors like C++, but the difference lies in the fact that Java does not create a
default copy constructor if you do not write your own.
Composition is exactly like Aggregation except that the lifetime of the ‘part’ is controlled by the ‘whole’. This
control may be direct or transitive. That is, the ‘whole’ may take direct responsibility for creating or destroying
the ‘part’, or it may accept an already created part, and later pass it on to some other whole that assumes
responsibility for it.
Sample class Car is shown below to demonstrate Composition of tires, doors, windows and steering.
}
class Door
{
}
class Steering
{
}
class Window
{
}
that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the
external method, are also reflected in all places.
Pass by Value:
public class ComputingEngine
{
public static void main(String[] args){
int x = 15;
ComputingEngine engine = new ComputingEngine();
engine.modify(x);
System.out.println("The value of x after passing by value "+x);
}
public void modify(int x){
x = 12;
}
}
Below example shows pass by reference in the code.
Pass by Reference:
public class ComputingEngine{
public static void main(String[] args){
ComputingEngine engine = new ComputingEngine();
Computation computation = new Computation(65);
engine.changeComputedValue(computation);
System.out.println("The value of x after passing by reference "+ computation.x);
}
class Computation{
int x;
Computation(int i) { x = i; }
In a singleton class we:
i. ensure that only one instance of the singleton class ever exists
ii. provide global access to that instance
Enumeration is twice as fast as compared to an Iterator and uses very less memory. However, the Iterator is
much safer compared to Enumeration, because other threads are not able to modify the collection object that
is currently traversed by the iterator. Also, Iterators allow the caller to remove elements from the underlying
collection, something which is not possible with Enumerations.
These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to
the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.
Sample class ReferenceObject is shown below to demonstrate the usage of System.gc and Runtime.gc
methods.
Runtime.gc();
}
}
public class MainClass{
public static void main(String[] args){
String s1 = "JavaJ2eeStrutsHibernate";
String s2 = "StrutsHibernateJavaJ2ee";
//Step 1
if(s1.length() != s2.length()){
System.out.println("s2 is not rotated version of s1");
}
else{
//Step 2
String s3 = s1 + s1;
//Step 3
if(s3.contains(s2)){
System.out.println("s2 is a rotated version of s1");
}
else{
System.out.println("s2 is not rotated version of s1");
}
}
}
}
private static void characterCount(String inputString){
//Creating a HashMap containing char as a key and occurrences as a value
charCountMap.put(c, charCountMap.get(c)+1);
}
else{
//If char 'c' is not present in charCountMap,
//putting 'c' into charCountMap with 1 as it's value
charCountMap.put(c, 1);
}
}
System.out.println(inputString+" : "+charCountMap);
}
characterCount("All Is Well");
//Output
if(status){
System.out.println(s1+" and "+s2+" are anagrams");
}
else{
System.out.println(s1+" and "+s2+" are not anagrams");
}
}
isAnagram("keEp", "peeK");
isAnagram("Toss", "Shot");
isAnagram("joy", "enjoy");
}
}
if (inputStringArray[i] == ' '){
resultArray[i] = ' ';
}
}
int j = resultArray.length-1;
resultArray[j] = inputStringArray[i];
j--;
}
}
Step 2 : Read the image using ImageIO.read() method into BufferedImage object.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
try{
image = ImageIO.read(file);
System.out.println("done");
}
}
import java.io.File;
files.close();
}
}
OR
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
files.forEach(System.out::println);
files.close();
}
}
Yes, static nested classes can contain both static and non-static members.
class OuterClass{
//Some members of OuterClass
void methodOne(){
//Non-static method
}
class Shared
{
static void staticMethod()
{
synchronized (Shared.class)
{
//static synchronized block - 1
}
synchronized (Shared.class)
{
//static synchronized block - 2
}
}
void NonStaticMethod()
{
synchronized (this)
{
//Non-static Synchronized block - 1
}
synchronized (this)
{
//Non-static Synchronized block - 2
}
}
}
Concept Tested
Java Collection
Java Basic
Java Collection
Java Collection
Java Collection
Java Collection
Java IO and NIO
Java IO and NIO
Java IO and NIO
Nested Class
Nested Class