Download as xlsx, pdf, or txt
Download as xlsx, pdf, or txt
You are on page 1of 100

A

E
ACE
D
AD
A
A
D
AC
BD
A
D
C
BC
ABC
C
B
AC
C
B
D
C
B
F
AB
E
AC

AC
A
AD
CEF
BE
A
BE
A
A
A

D
A
B
E
AC
D
B
B
A
C
A
C
A
C
C
C
C
B
A
A
ABC
B
C
C
D

A
B
A
D
D
A
E
D
D
AC
D
A
A
E

ABE
C
BE

ADF

B
(But, 12 is
missing)
B
B
E
A
A

E
C
C
?
AB
A
CE
?
B
E

A
A

A
A

BC
C

B
BCE
E
A

ABC
E

C
C
C

C
A
BD

CE

B
AD

C
C
C

C
CD

A
C
C
ABD

A
A

E
C
B
C
A
B

D
C
CD
C
AD
A
A

A
C
B
C

A
C
E
D
C

B
E
C
D

C
E
C
D
D

C
D

C
C
B

A
Only I and III
A
B

C
C
B
ADE

?
C
B
B
D
C
C
D
A
D
C
C
C
C
CD

D
C

BD
B

C&D-?
BD
AE
BD
B
C
C

A
B

B
D

B
AD
CD
A
A
C
D
C

E
C
C
Document
shows it as G

E
A
A
B
B

A
A

A
C

AC
ABF
E
B
DGH

D
10:10
exception

output: ABCC

Output:
c=
b = false
f = 0.0
Output:
Java
Jeve
va

Output:
Super
Sub
Sub

Output:
Super
Sub
Sub

Shining Sun
Shining Sun
Found Default

public class Test1 {


int fvar;
static int cvar;
public static void main(String[] args){
Test1 t = new Test1();
//t.fvar = 200;
//cvar = 400;
//Test2.cvar =400;
//System.out.println(t.fvar); //200
//System.out.println(cvar); //400
//System.out.println(Test2.cvar); //400
}
}
LocalTime.now()
Subclasses of Exception except Runtime Exception
Low to high - acceptable
but high to low - unacceptable

public class Test1 {


public static void main (String args[]) {
int arr[] = new int[4];
arr[0]=1;
arr[1]=2;
arr[2]=4;
arr[3]=5;
int sum =0;
try {
for (int pos=0; pos<=4; pos++) {
sum = sum + arr[pos];

}
}
catch (Exception e) {
System.out.println("invalid index");

}
System.out.println(sum);
}
}

Output:
invalid index
12
public class Test1 {
public static void main (String args[]) {
boolean log3 = (5.0 != 6.0) && (4 != 5);
boolean log4 = (4!=4) || (4==4);
System.out.println("log3:"+log3+"\nlog4:"+log4);
}
}

Output:
log3:true
log4:true

public class Test1 {


public static void main (String args[]) {
int result = 30 - 12 / (2*5) + 1;
System.out.println("Result:"+result);
}
}

Output:
Result:30
class A {
public A(){
System.out.print("A");
}
}

class B extends A {
public B(){
System.out.print("B");
}
}

public class Test1 extends B {


public Test1() {
System.out.print("C");
}

public static void main (String args[]) {


Test1 t = new Test1();
}

}
Output: ABC
}
}
}
Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Test1.main(Test1.java:15)

C)
public class Test1 {
public static void main (String args[]) {
String[][] arra = new String[3][];
arra[0]=new String[]{"rose", "lily"};
arra[1]=new String[]{"apple","berry","cherry","grapes"};
arra[0]=new String[]{"beans","carrot","potato"};
for (String a[]:arra[][]){
for (String x:a[]){
toUpperCase();
}
}
}
}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token(s), misplaced construct(s)
Can only iterate over an array or an instance of java.lang.Iterable
Syntax error on token "[", Expression expected after this token
The method toUpperCase() is undefined for the type Test1

at Test1.main(Test1.java:17)

D)
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Can only iterate over an array or an instance of java.lang.Iterable
Type mismatch: cannot convert from element type String[] to String
Cannot invoke toUppercase() on the array type String[]

at Test1.main(Test1.java:21)
public void m2(){
System.out.println("Green");
}

abstract class A2 extends A1{


public abstract void m3();
public void m1(){
System.out.println("Cyan");
}
public void m2(){
System.out.println("Blue");
}
}

public class Test2 extends A2{


public void m1(){
System.out.println("Yellow");
}
public void m2(){
System.out.println("Pink");
}
public void m3(){
System.out.println("Red");
}
public static void main(String args[]){
A2 tp = new Test2();
tp.m1();
tp.m2();
tp.m3();
}

}
Output:
Yellow
Pink
Red
DBConfiguration dbconf = r.configureDB("manager","manager");

}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
DBConfiguration cannot be resolved to a variable

at Test1.configureDB(Test1.java:10)
at Test1.main(Test1.java:15)
C)
class DBConfiguration {
String user;
String password;
}

public class Test1 {

DBConfiguration configureDB (String uname, String password){


System.out.println("configureDB");
return new DBConfiguration; //Option C

}
public static void main (String args[]){
Test1 r = new Test1();
DBConfiguration dbconf = r.configureDB("manager","manager");

}
}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "new", delete this token
DBConfiguration cannot be resolved to a variable

at Test1.configureDB(Test1.java:10)
at Test1.main(Test1.java:15)
Syntax error in while loop statement
public class Test1 {
static void dispResult(int[] num){
try {
System.out.println(num[1]/(num[1]-num[2]));
} catch (ArithmeticException e){
System.err.println("first exception");
}
System.out.println("Done");
}

public static void main (String args[]){


try {
int[] arr= {100, 100};
dispResult(arr);
}catch (IllegalArgumentException e){
System.err.println("Second exception");
}catch (Exception e){
System.err.println("third exception");
}
}
}

Output: third exception

public class Test1 {


public static void main (String args[]){
int numbers[];
numbers = new int[2];
numbers[0]=10;
numbers[1]=20;

numbers = new int[4];


numbers[2]=30;
numbers[3]=40;
for (int x:numbers){
System.out.print(" "+x);
}
}
}
Output:
0 0 30 40
Replace line 5 with String opt = "true";
Replace line 7 with case "true":
class Alpha{
public String[] main = new String[2];
Alpha(String[] main){
for (int ii = 0; ii<main.length; ii++){
this.main[ii]=main[ii]+5;
}
}
public void main() {
System.out.print(main[0]+main[1]);
}
}
public class Test1 {
public static void main (String args[]){
Alpha main = new Alpha(args);
main.main();
}
}

Java Test1 1 2

Output: 1525

public class Test1 {


public static void main (String args[]){
int aVar = 9;
if (aVar++<10)
System.out.println(aVar+" Hello World");
else
System.out.println(aVar+" Hello World");
}
}
Output: 10 Hello World
Read explanation

public class Test1 {


public static String maskCC(String creditCard){
String x = "XXXX-XXXX-XXXX-";
//return x+creditCard.substring(15,19); //Option B - Output: XXXX-XXXX-XXXX-1121
StringBuilder sb = new StringBuilder(x);
sb.append(creditCard, 15,19);
return sb.toString(); //Option C - Output: XXXX-XXXX-XXXX-1121
}
public static void main (String[] args) {
System.out.println(maskCC("1234-5678-9101-1121"));
}
}
public class Test1 {
static boolean bVar;
public static void main (String[] args) {
boolean bVar1 = true;
int count =8;
do{
System.out.println("Hello Java!"+count);
if (count >=7){
bVar1=false;
}
}while ((bVar!=bVar1) && (count > 4));
count -=2;
}
}
Output: Hello Java!8

question is how many objects created - 1 or 2? i thnk answer is 1, bcz only one "new instance" available. Obj2 also point to obj

If you run this program,


public class Test1 {
int num;
public static void graceMarks(Test1 obj4){
obj4.num+=10;
}

public static void main (String[] args) {


Test1 obj1 = new Test1();
Test1 obj2 = obj1;
Test1 obj1 = null;
obj2.num = 60;
graceMarks(obj2);
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable obj1

at Test1.main(Test1.java:11)
import java.util.ArrayList;
public class Test1 {
public static void main (String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("SE");
list.add("EE");
list.add("ME");
list.add("SE");
list.add("EE");
list.remove("SE");
System.out.println("Values are:"+ list);
}
}
Output:
Values are:[EE, ME, SE, EE]
5:5
Option D is in correct bcz variable i is not declared/inialized
option E is incorrect bcz int is missing in statement i=0;

import java.util.ArrayList;
public class Test1 {
public static void main (String[] args) {
ArrayList myList= new ArrayList(1);
String[] myArray;
try{
while(true){
myList.add("MyString");
}
} catch (RuntimeException re) {
System.out.println("Runtime exception");
} catch (Exception e) {
System.out.println("Caught an exception");
}
System.out.println("Ready to use");
}
}
Output:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2245)
at java.util.Arrays.copyOf(Arrays.java:2219)
at java.util.ArrayList.grow(ArrayList.java:242)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
at java.util.ArrayList.add(ArrayList.java:440)
at Test1.main(Test1.java:8)
String[] d = {"a","b","c"};
update(d);
for (String s : d){
System.out.println(s+" ");
}
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method update(String[]) from the type Test1 refers to the missing type List
at Test1.main(Test1.java:14)

Option B:
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at Test1.main(Test1.java:12)

Option C:
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method update(String[]) from the type Test1 refers to the missing type List

at Test1.main(Test1.java:14)

Option D:
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at Test1.main(Test1.java:12)

Option E:
output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method update(String[]) is undefined for the type Test1

at Test1.main(Test1.java:14)
public class Test2 {
int x;
int y;
public void doStuff(int x, int y){
this.x = x;
y=this.y;
}
public void display(){
System.out.print(x+" " + y + ": ");
}

public static void main (String args[]){


Test2 m1 = new Test2();
m1.x = 100;
m1.y = 200;
Test2 m2 = new Test2();
m2.doStuff(m1.x, m1.y);
m1.display();
m2.display();
}
}
output:
100 200: 100 0:

public class Test1 {

public static void main (String[] args) {


int b=3;
if (!(b>3)){
System.out.println("square");
}{
System.out.println("circle");
}
System.out.println("...");

}
}
Output:
square
circle
...
public class Test1 {
public static void main (String[] args) {
StringBuilder sb1 = new StringBuilder("Duke");
String str1 = sb1.toString();
String str2 = str1; //If we insert Option A - Line 10 will print - true
//String str2 = new String(str1);//If we insert Option B - Line 10 will print - false
//String str2 = sb1.toString(); //If we insert Option C - Line 10 will print - false
//String str2 = "Duke"; //If we insert Option D - Line 10 will print - false
System.out.println(str1 == str2);
}
}

public class Test1 {


public static void main (String[] args) {
//int[][] arr = new int[2][]; // will compile good
//int[][] arr = new int[2][0]; // will compile good
arr[0] = new int[3];
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1]=new int[4];
arr[1][0]=10;
arr[1][1]=20;
arr[1][2]=30;
arr[1][3]=40;
}
}

public class Test1 {


public static void main (String[] args) {
String str1 = "Java";
char str2[]={'J','a','v','a'};
String str3 = null;
for (char c : str2) {
str3 = str3 + c;
}
//I added this line - System.out.println("str1 has "+str1); //Java
//I added this line - System.out.println("str3 has "+str3); //nullJava
if (str1.equals(str3)) //str1 content not matching with str2 so it returns false
System.out.println("successful");
else
System.out.println("Unscuccessful");
}
}

Output: Unscuccessful
class Caller {
private void init(){
System.out.println("Iniatialized");
}
public void start(){
init();
System.out.println("Started");
}
}
public class Test2 {
public static void main (String args[]){
Caller c = new Caller();
c.start();
c.init();
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method init() from the type Caller is not visible

at Test2.main(Test2.java:14)

import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main (String[] args) {
List<Integer> list = new ArrayList<>();
list.add(21); list.add(13);
list.add(30); list.add(11);
list.add(2);
list.removeIf(e -> e%2 = 0);
System.out.println(list);
}
}

Output: [21 13 11]


public class Test1 {
public static void main (String[] args) {
int[] intArr = {15,30, 45,60,75};
intArr[2]=intArr[4];
intArr[4]=90;
for (int i:intArr){
System.out.print(i+" ");
}
}
}

Output: 15 30 75 60 90

class SpecialException extends Exception {


public SpecialException (String message){
super(message);
System.out.println(message);
}
}
public class Test2 {
public static void main (String args[]){
try{
doSomething();
}catch(SpecialException e){
System.out.println(e);
}
}

static void doSomething () throws SpecialException{


int[] ages = new int[4];
ages[4]= 17;
doSomethingElse();
}

static void doSomethingElse() throws SpecialException{


throw new SpecialException ("Thrown at end of doSomething() method");
}
}
output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Test2.doSomething(Test2.java:17)
at Test2.main(Test2.java:10)
abstract class Planet {
protected void revolve(){
}
abstract void rotate();
}
class Test1 extends Planet {
protected void revolve(){ //option D & C works - we can place public instead of protected, still code works
}
protected void rotate(){
}

public static void main (String args[]){


System.out.print("compiled"); // I added this code to make sure it compiles
}
}

class Test1 {
static int i = 7;
public static void main (String args[]){
Test1 obj = new Test1();
obj.i++;
Test1.i++;
obj.i++;
System.out.print(Test1.i+" "+obj.i);
}
}
Output: 10 10

class Tours{
public static void main(String[] args){
System.out.print("Happy Journey! "+args[1]);
}
}

public class Test1 {


public static void main(String[] args){
String[] s = new String[2]; // i added this line to run this in eclipse
s[0]="Java"; // i added this line to run this in eclipse
s[1]="Duke"; // i added this line to run this in eclipse
Tours.main(s); // changed args to s
}
}

Output: Happy Journey! Duke


Only one "new instance" is there

public class Test1 {


public static void main(String[] args){
int num = 5;
int sum;
do{
sum +=num;
}while((num--)>1);
System.out.println("The sum is "+sum+".");
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable sum may not have been initialized

at Test1.main(Test1.java:8)

Exception in thread "main" java.lang.NumberFormatException: For input string: "808.1"


at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.valueOf(Integer.java:582)
at Test1.main(Test1.java:4)

BD ? - Saravana
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args){
List<String> names = new ArrayList<>();
names.add("Robb");
names.add("Bran");
names.add("Rick");
names.add("Bran");

if (names.remove("Bran")){
names.remove("Jon");
}
System.out.println(names);
}
}
Output: [Robb, Rick, Bran]

public class Test1 {


public static void main(String[] args){
for (int ii=0; ii<3; ii++){
int count =0;
for (int jj =3; jj>0; jj--) {
if (ii==jj){
++count;
break;
}
}
System.out.print(count);
continue;
}
}
}
Output: 011

C? - Saravana
Interface has default method which should have method body

public class Test1 {


public static void main(String[] args){
float x = 22.00f % 3.00f;
int y = 22 % 3;
System.out.println(x+" "+y);
}
}
Output: 1.0 1
public class Test1 {
private int x=0;
private int y=0;
public static void main(String[] args){
Test1 accApp = new Test1();
accApp.printThis(1,2);
accApp.printThas(3, 4);
}
public void printThis(int x, int y){
x = x;
y = y;
System.out.println("x: "+this.x+" y: "+this.y);
}
public void printThas(int x, int y){
this.x = x;
this.y = y;
System.out.println("x: "+this.x+" y: "+this.y);
}
}
Output:
x: 0 y: 0
x: 3 y: 4
check spelling of ArrayLIst in option E
output: Infinite loop

public class Test1 {


public static void main (String args[]) {
float myarray[] = { 10.20f, 20.30f, 30.40f, 50.60f};
int index=0;
boolean isFound = false;
float key = 30.40f;
while (index++ < 5){
if (key ==myarray[index]){
isFound = true;
}
}
System.out.println(isFound);
}
}
output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Test1.main(Test1.java:10)

public class Test1 {


public static void main (String args[]) {
float myarray[] = { 10.20f, 20.30f, 30.40f, 50.60f};
int index=0;
boolean isFound = false;
float key = 30.40f;
while (index < 5){ //option D
if (key ==myarray[index]){
isFound = true;
break;
}
index++;
}
System.out.println(isFound);
}
}
Output: true
public class Test1 {
public static void main (String args[]) {
int ax = 10, az=30;
int aw = 1, ay =1;
try{
aw=ax%2;
ay=az/aw;
}catch(ArithmeticException e1){
System.out.println("Invalid Divisor");
}catch(Exception e2){
aw=1;
System.out.println("Divisor changed");
}
ay=az/aw;
System.out.println("Successful Division"+ay);
}
}
Output:
Invalid Divisor
Exception in thread "main" java.lang.ArithmeticException: / by zero at Test1.main(Test1.java:14)

public class Test1 {


public static void main (String args[]) {
String s = "A";
switch(s){
case "a":
System.out.print("simaple A");
default:
System.out.print("default");
case "A":
System.out.print("Capital A");
}
}
}
Output: Capital A
public class Test1 {
public static void main (String args[]) {
try{
Double number = Double.valueOf("120D");
}catch(NumberFormatException ex){

}
System.out.println(number);
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
number cannot be resolved to a variable
at Test1.main(Test1.java:8)

public class Test1 {


public static void main (String args[]) {
int a = -10;
int b = 17;
int c = ++a;
int d = b--;
c++;
d--;
System.out.print(c+" , "+d);
}
}
output: -8 , 16

public class Test1 {


public static void main (String args[]) {
Short s1 = 200;
Integer s2 = 400;
Long s3 = (long) s1 + s2;
String s4 = (String) (s3*s2);
System.out.println("Sum is "+s4);
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot cast from long to String

at Test1.main(Test1.java:6)
public class Test1 {
public static void main (String args[]) {
int[] xx=null;
for (int ii : xx){
System.out.println(ii);
}
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at Test1.main(Test1.java:4)

public class Test1 {


public static void main (String args[]) {
Test1 ts = new Test1();
System.out.print(isAvailable+" ");
isAvailable = ts.doStuff();
System.out.print(isAvailable);
}
public static boolean doStuff(){
return !isAvailable;
}
static boolean isAvailable = false;
}
Output:
false true

class MyString {
String msg;
MyString(String msg){
this.msg=msg;
}
}
public class Test1 {
public static void main (String args[]) {
System.out.println("Hello "+ new StringBuilder("StringBuilder Java SE 8"));
System.out.println("Hello "+ new MyString("Java SE 8"));
}
}
Output:
Hello StringBuilder Java SE 8
Hello MyString@fb53f6
public class Test1 {
public static void main (String args[]) {
System.out.println( 28 + 5 <= 4 + 29 );
System.out.println( (28 + 5) <= (4 + 29) );
}
}
output:
true
true

public class Test1 {


public static void main (String args[]) {
Boolean[] bool = new Boolean[2];

bool[0]=new Boolean(Boolean.parseBoolean("true"));
bool[1]=new Boolean(null);

System.out.println(bool[0]+" "+bool[1]);
}
}
output:
true false
E?

public class Test1 {


static double dvalue;
static Test1 ref;
public static void main (String args[]) {
System.out.println(ref);
System.out.println(dvalue);
}
}
output:
null
0.0
public class Test1 {
String myStr = "7007";
public void doStuff(String str){
int myNum = 0;
try{
String myStr = str;
myNum = Integer.parseInt(myStr);
}catch(NumberFormatException ne){
System.err.println("Error");
}
System.out.println("myStr: "+myStr+" myNum: "+myNum);
}

public static void main (String args[]) {


Test1 obj = new Test1();
obj.doStuff("9009");
}
}
output:
myStr: 7007 myNum: 9009

public class Test1 {


public static void main (String args[]) {
Integer num = Integer.parseInt(args[1]);
System.out.println("Number is : "+num);
}
}
public class Test1 {
public static void main (String args[]) {
float fit = 100F;
float fit1=(float) 1_11.00;
float fit2=100;
double y1 = 203.22;
float fit3 = y1; // error on this line
int y2=100;
float fit4=(float)y2;
}
}
output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from double to float

at Test1.main(Test1.java:7)

B? - Saravana

AD - Saravana ?

public class Test1 {


private int i;
void disp(){
while(i<=5){
for (int i=1;i<=5;){
System.out.print(i+" ");
i++;
}
i++;
}
}
public static void main(String[] args){
new Test1().disp();
}
}
output:
12345 12345 12345 12345 12345 12345
public class Test1 {
public static void main(String[] args){
int var1 = -5;
int var2 = var1--;
System.out.println("after var2 = var1--");
System.out.println("var2: "+var2);
System.out.println("var1: "+var1);
int var3 = 0;
if (var2 < 0){
var3 = var2++;
System.out.println("after var3 = var2++");
System.out.println("var3: "+var3);
System.out.println("var2: "+var2);
}else{
var3 = --var2;
}
System.out.println("var3: "+var3);
}
}
output:
after var2 = var1--
var2: -5
var1: -6
after var3 = var2++
var3: -5
var2: -4
var3: -5

C is not correct as class Z does noit implement abstract methodY which is from interface
D is correct as if the subclass is abstract, then sub class is no need to implement abstract methods from adstract class &
interface
E - Saravana ?

public class Test1 {


public static void main(String[] args){
String[] planets = {"Mercury", "Venus", "Earth", "Mars" };

System.out.println(planets.length);
System.out.println(planets[1].length());
}
}
output:
4
5
import java.awt.List;
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args){
List colors = new ArrayList();
colors.add("green");
colors.add("red");
colors.add("blue");
colors.add("yellow");
colors.remove(2);
colors.add(3, "cyan");
System.out.println(colors);
}
}
output:
[green, red, yellow, cyan]

public class Test1 {


public static void doSum(Integer x, Integer y){
System.out.println("Integer Sum is: "+(x+y));
}
public static void doSum(double x, double y){
System.out.println("double Sum is: "+(x+y));
}
public static void doSum(float x, float y){
System.out.println("float Sum is: "+(x+y));
}
public static void doSum(int x, int y){
System.out.println("int Sum is: "+(x+y));
}
public static void main(String[] args){
doSum(10, 20);
doSum(10.0, 20.0);
}
}
Output:
int Sum is: 30
double Sum is: 30.0
public class Test1 {
public static void main(String[] args){
String[] strs = new String[2];
int idx=0;
for (String s : strs){
strs[idx].concat(" element "+idx);
idx++;
}
for (idx=0; idx<strs.length; idx++){
System.out.println(strs[idx]);
}
}
}
output:
Exception in thread "main" java.lang.NullPointerException
at Test1.main(Test1.java:6)

public class Test1 {


public static void main(String[] args){
String message = "Hi everyone!";
System.out.println("message= "+message.replace("e", "X"));
}
}
Output:
message= Hi XvXryonX!

C - Saravana?
interface Z {
}

class Test1 implements Z {


public String toString(){
return "X ";
}
public static void main(String args[]){
Y myY = new Y();
Test1 myX = myY;
Z myZ = myX;
System.out.println(myX);
System.out.println((Y)myX);
System.out.println(myZ);
}
}

class Y extends Test1{


public String toString(){
return "Y ";
}
}
output:
Y
Y
Y
I think C

public class Test2 {


public static void main(String[] args){
int x = 100;
int a = x++;
int b = ++x;
int c = x++;
int d = (a < b) ? (a < c) ? a : (b < c) ? b : c;
System.out.println(d);
}
}
output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert ": Expression" to complete Expression
at Test2.main(Test2.java:7)
Output:
1200 Strawberry
1230 Chocolate

if u see options after the statement - what is the result , it's option C.

public class Test2 {


public static void main(String[] args){
int array[] = new int[-2];
}
}
output:
Exception in thread "main" java.lang.NegativeArraySizeException
at Test2.main(Test2.java:3)

class Vehicle {
String type = "4W";
int maxSpeed = 100;
Vehicle (String type, int maxSpeed){
this.type = type;
this.maxSpeed = maxSpeed;
}
}
public class Test2 extends Vehicle {
String trans;
Test2 (String trans){
this.trans = trans;
}
Test2 (String type, int maxSpeed, String trans){
super(type, maxSpeed);
this(trans);
}
public static void main(String[] args){
Test2 c1 = new Test2("Auto");
Test2 c2 = new Test2("4W", 150, "Manual");
System.out.println(c1.type+" "+c1.maxSpeed+" "+c1.trans);
System.out.println(c2.type+" "+c2.maxSpeed+" "+c2.trans);
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Implicit super constructor Vehicle() is undefined. Must explicitly invoke another constructor
Constructor call must be the first statement in a constructor
at Test2.<init>(Test2.java:11)
at Test2.main(Test2.java:19)
public class Test2 {
public static int main(String[] args){
String[] s = new String[1];
s[0] = "Sathes";
s[1] = "kumar";
System.out.println(s[1]);
return 0;
}
}
Output:
Error: Main method must return a value of type void in class Test2, please
define the main method as:
public static void main(String[] args)

public class Test2 {


public static void main(String[] args){
int[] ar1={2,4,6,8};
int[] ar2={1,3,5,7,9};
ar2 = ar1;
for (int e2 : ar2){
System.out.print(" "+ e2);
}
}
}
output:
2468

to compile source file - javac Main.java


to run class file(byte code) - java Main

public class Test2 {


public static void main(String[] args){
Object array[];
Boolean array1[3];
int[] array3;
Float[] array4[4];
}
}
output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "3", delete this token
Syntax error on token "4", delete this token

at Test2.main(Test2.java:4)
public class Test2 {
public static void main(String[] args){
StringBuilder sb = new StringBuilder(5);
String s = "";
if (sb.equals(s)){
System.out.println("Match 1");
} else if (sb.toString().equals(s.toString())){
System.out.println("Match 2");
} else {
System.out.println("No match");
}
}
}
output: Match 2

public class Test2 {


int x (double d){
System.out.println("one");
return 0;
}

String x (double d){


System.out.println("two");
return null;
}

double x (double d){


System.out.println("three");
return 0.0;
}

public static void main(String args[]){


new Test2().x(4.0);
}

}
Output:
one
class A {
public A(){
System.out.print("A");
}
}

class B extends A {
public B(){
super();
System.out.print("B");
}
}

public class Test1 extends B {


public Test1() {
super();
System.out.print("C");
}

public static void main (String args[]) {


Test1 t = new Test1();
}

}
Output: ABC
tried both B & C - both got exception
not sure - have to run once in command line
1 or 2?
none of the option compiles, all giving error. If we match return type of data (List) with
return type of method Update, then option C is correct
have to run this in command line with Javac & Java

D ah ?
Option C/G - partially correct
Given options has no exact output
See explanation
String myStr = str;
This above assignment is out of scope after try block.

have to run this in command line with Javac & Java


See explanation
have to run this in command line with Javac & Java
B- Note: if the change made in option B - 30.0
Correct Code: (Fixed)
class Vehicle {
String type;
int maxSpeed;
Vehicle(){
type = "4W";
maxSpeed = 100;
}
Vehicle (String type, int maxSpeed){
this.type = type;
this.maxSpeed = maxSpeed;
}
}
public class Test2 extends Vehicle {
String trans;
Test2 (String trans){
super();
this.trans = trans;
}
Test2 (String type, int maxSpeed, String trans){
super(type, maxSpeed);
this.trans = trans;
}
public static void main(String[] args){
Test2 c1 = new Test2("Auto");
Test2 c2 = new Test2("4W", 150, "Manual");
System.out.println(c1.type+" "+c1.maxSpeed+" "+c1.trans);
System.out.println(c2.type+" "+c2.maxSpeed+" "+c2.trans);
}
}
Correct Code: (Fixed)Output:
4W 100 Auto
4W 150 Manual
have to run this in command line with Javac & Java

I think it's A

Correct declaration:
public class Test2 {
public static void main(String[] args){
Object array[];
Boolean array1[] = new Boolean[3];
int[] array3;
Float[] array4 = new Float[4];

}
}
A A A
E E E
A, C, E A, C, E A, C, E
D D D
A, D A, D A, D
A A A A
A A A
D D D D
A, C A, C A,C A, C
B, D B, D B, D
A A C A(10, 8, 6, 4, 2, 0)
D D D D int f = ps.index(p2)
C C C C
A, B A,B A, B

A class can have only one private constructor.


A method can have same name as a variable
A, B, C B,C,F A, B, C A class can have overloaded static methods
B B,D B Add throws Exception in main method
B B B B
C, D C,D C, D
C C C C false: true

ns = 50; s = 125
ns = 125; s = 125
B B B B ns = 0; s = 100
D D D caught: java.lang.RunTime Exception (only as doPrint() will throw the exception
C C C Ternary operation (x>10 ? ">" : x<10 ? "<" : "=")
B B B null will get printed when no assignment is made to the array (Null Unix Linux So

Square square = new Square();


square.foo("bar");
F F F square.foo();
A, B A, B A, B index > 0 and index-- OR --index
E D E infinte loop (while i<= var) never terminates
Security Exception
A, C A, C A,C A, C Illegal Argument Exception
C C C ABCC
A, C A, C A,C A, C
A A A

Changing the access modifier of variable from public


to private
Return a copy of contents of an array or list instead
A, D A, D A, D of direct reference
C, E, F C,E,F C, E, F Sum field and 2 methods that updates the Sum
B, E B, E B,E B, E
A A A Encapsulation ensure that classes can be designed so that only certain fields and
B, E B,C B, E
97, 98
A A A A 99, 100, null, null, null
A A A 1:2:3:4:5:
A A A 11, 11 (iObj++, iVar++)

char will be printed as empty;


c=
b = false
E E E E f = 0.0
C D B,D D B and D are correct; shirt colors and sizes
A A A

java
jeve
D B C va
E E E
A, C A,C A, C
D D D D Compilation fails only at n3. Runtime exception is not required to be handled
B B B Failure
B B B
A A A Stringbuilder.insert()
C D C C false false (isEmpty prints false when there is a space inside)
E E E
C C C C
A A A
C D C
C C C C Planets[2] will print the reference of array in index 2
C C C 123xxx
C C C
B B B
A A A A var + Hello World! (10 Hello World)
D A A
A, b A,B,C ABC
B B B
C C C
C C C Java byte code can run on any platform that has JRE
D B D int can be used as reference type for arrays
A A A
A A A
B B B B
A A A A
D D D
D D D
A A A
A B E Orange compilation fails
D D D
D D D D
A, C A, C A,D AC PASS NEW()
D D D Compile error will be shown only at the usage not at declaration
D A A
A A A
E E E
A, B A, B ABE
C C C

Checked exceptions are exceptional conditions that


are external to the application, and that the
application usually cannot anticipate or recover
from.
It is subclass of Exception, excluding RunTime and its
B, E B BE subclasses
A, D, F A, D, F A,D,F ADF assigning a double value to int needs casting even though the value doesn't cont

B
(But, 12 is
A, B A missing) 12, Invalid Index
B B B
B B B
E E E
A A A
A A A
E E E
C C C
C C C C
A A A
A, B A,B AB
A A A
C, E C, E C,E CE
B B B
B A B While loop syntax is incorrect
B B E
B B B
A A A A
A A A
A A A
E A A A Only A.java file compiles successfully (B.java contains private variable inside met

//return x+creditCard.substring(15,19); //Option B -


Output: XXXX-XXXX-XXXX-1121
StringBuilder sb = new StringBuilder(x);
sb.append(creditCard, 15,19);
return sb.toString(); //Option C - Output: XXXX-
A, B B, C A,B BC XXXX-XXXX-1121
D C C Hello Java!8
C B A B How many objects are created? 2
B, C, E B,D,E BCE
E E E
A A A
A, B, C A,B,C ABC
E E E E
D C C C while(true) Infinite loop - run time error
A C C static List update (since the list is declared as static)
C A C 100 200 100 0
C A C squarecircle
A A A
D, E B, D B,D BD Alternate order of processing can be done with standard for loop
C, E C,E CE
B A B Unsuccessful
A, D A,D AD
C B C private method
C C C
C C C
javac <ClassName>.java
C C C C java <ClassName> args[]
C A C
C, D C, D C,D CD n3 public and protected
A A A
B B C How many objects are created? 4 (new Dog example)
A C C public static void main (String[] args)

Encapsulation -
Allows a class implementation to change without
changing the clients
Protects confidential data from leaking out of objects
A, D, F A,B,D ABD Enables class implementaion to protect its variants
A A A
A A A A How many marklist instances are created? 1
C C E Compilation error - variable sum might not have been initialized
C C C C
B B B
A C C no argument class Z initializes varibable X3 (since the instanct is created for that
A A A
B B B B
B D D Number format exception while parsing(808.1)
A D C
C, D C,D CD
C C C C
A A,D AD protected variables are also considered as encapsulated
A A A A Toy calculation - abstract calculation method
A A A A remove from list (removes only one occurrence)
B A A 011 - count resets to 0
D C C use .delete to empty all contents of Stringbuilder
B B B
C C C
B B A 1.0, 1 (float variable)
x: 0 y: 0
B B C x: 3 y: 4
D E E E check spelling of ArrayLIst in option E
D D D
C C C
B B B
Compilation fails since the number variable is
C C E declared inside the try block
C C C
D D D
B B B
E E E C Compilation fails while multiplying number and assigning to a string
C E
C C
B D
D D
E C C C false true (variable can be declared at the end)
D D
C C
C C
B B
A A
E Only I and III
A A A A it accesses private method
B B
D C Null 0.0
C C C C myStr: 7007, myNum:9009
D B
A, D, E ADE
B ?
D C
D B Lions is always jumping; it's a static variable which is intialized once the main me
A B a,e o,o
D D D D double d = 203.22; float f = d; compilation error occurs
C C
C C
B D 10, 20, 30, 10, 20, 30
A A
D D
C C
C C
B C key is used outside of the loop
Out of limits
D C hEllO jAvA
A, D CD
C D 12345 printed 6 times
A C -5
B, D BD
B B
planets.length - 4
B B B C & D - ? planets[0].lengh - 5
B, C, D BD double and float; int can't be pointed to float calculation
A, E AE
B, D BD
B B
B C
C C C C A, B, D, E (while 'B' - break;)
A A A A green, red, yellow, cyan
B B B B At line 7, insert --x;
int sum is 30
B B B B double sum is 30.0
C D D D Null pointer will be thrown while printing an empty/null string
Replace in string replaces all characters
F B Hi XvXryonx!;
public static void main
A, D A, D A,D AD static public void main

Readbook, Setbook
change n3 to abstract class
C, D C, D D CD insert method declaration in n4
A A
A A
C C
D D
B C
E E E E Invalid Ternary operation. Compilation fails
C C
C C

Documen
t shows it
G as G
C B NegativeArraySizeException

Compilation fails at line n1 and n2 (Vehicle example)


Implicit super constructor Vehicle() is undefined.
Must explicitly invoke another constructor
Constructor call must be the first statement in a
E E E E constructor
A A

num[0][0] = 10
num[0][1] = 10
A A A A num[0][2] = 10
B B
B B B B Code compiles but does not execute
A A
A A
B, D A javac Main.java
C C C C A work done
A, C AC
A, B, F ABF
B E Concat can't be used on Stringbuilder
D B Match 2

acct.amount = 0
acct.changeamount(-acct.amount)
D, F, G D, G, H D,G,H DGH acct.changeamount(-acct.getamount)
B D Compilation fails. Method signature can't be same
() will throw the exception first and exception will move to catch block

the array (Null Unix Linux Solaris)

o that only certain fields and methods of an object are accessible from other objects
t required to be handled

declaration
ough the value doesn't contain decimal point

private variable inside method and C.java doesn't contain matching class name - which are not allowed)
dard for loop

instanct is created for that)


ning to a string

intialized once the main method is called


A
E
A, C, E
D
A, D
A
A
D
A,C
B, D
A
D
C
A,B
B,C,F
B,D
B
C,D
C
B
D
C
B
F
A, B
D
A,C
C
A,C
A
A, D
C,E,F
B,E
A
B,C
A
A
A
E
B,D
A
B
E
A,C
D
B
B
A
C
E
C
A
D
C
C
C
B
A
A
A,B,C
B
C
C
B
A
A
B
A
D
D
A
B
D
D
A,D
D
A
A
E
A, B
C
B
A,D,F
A
B
B
E
A
A
E
C
C
A
A,B
A
C,E
B
A
B
B
A
A
A
A
A,B
C
A
B,D,E
E
A
A,B,C
E
C
C
A
A
A
B,D
C,E
A
A,D
B
C
C
C
A
C,D
A
B
C
A,B,D
A
A
C
C
B
C
A
B
D

C,D
C
A,D
A
A
A
C
B
C
B
B
E
D
C
B
C
C
D
B
C
E
C
D
D
C
D
C
C
B
A
Only III
A
B
D
C
B
B,C,F
B
D

C
A
B
B
D

A,D
D

A
B

D,G,H

You might also like