Practice Test 2
Practice Test 2
//============================================================== Q1:
public class Main {
public static void main(String args[]) {
int [] array = new int [3];
for (int a: array){
a = 100;
a++;
}
a ) 0 0 0
b ) 100 101 102
c ) 102 102 102
d ) None of the above
//============================================================== Q2:
public class Main {
public static void main(String args[]) {
int [] array = new int [3];
for (int a =0; i<3; i++){
array[a] = 100;
a++;
}
}
}
a ) 0 0 0
b ) 100 100 100
c ) 102 102 102
d ) None of the above
//============================================================== Q3:
public class Main {
public static void main(String args[]) {
Test t = new Test();
t.i = 200;
System.out.print(Test.i+" "+ t.i);
}
}
class Test{
private static int i;
public Test(){
i = 100;
}
}
a ) 100 100
b ) 100 200
c ) 200 100
d ) None of the above
//============================================================== Q4:
public class Main {
public static void main(String args[]) {
Test t = new Test();
t.i = 200;
System.out.print(Test.i+" "+ t.i);
}
}
class Test{
public static int i;
public Test(){
i = 100;
}
}
a ) 100 100
b ) 200 200
c ) 200 100
d ) None of the above
//============================================================== Q5:
public class Main {
public static void main(String args[]) {
Test.show();
}
class Test{
public void show(){
System.out.print("From Test");
}
}
a ) From Main
b ) From Test
c ) From TestFrom Main
d ) None of the above
//============================================================== Q6:
public class Main {
public static void main(String args[]) {
Test.show();
}
public static void show(){
System.out.print("From Main");
}
}
class Test{
public static void show(){
System.out.print("From Test");
}
}
a ) From Main
b ) From Test
c ) From TestFrom Main
d ) None of the above
//============================================================== Q7:
public class Main {
public static void main(String args[]) {
System.out.print(Test);
}
class Test{
public String toString (){
return "Test";
}
}
a ) Test
b ) "Test"
c ) None of the above
//============================================================== Q8:
public class Main {
public static void main(String args[]) {
Test t;
System.out.print(t);
}
class Test{
public String toString (){
return "Test";
}
}
a ) Test
b ) t
c ) None of the above
//============================================================== Q9:
public class Main {
public static void main(String args[]) {
Test t = new Test("test");
System.out.print(t);
}
class Test{
private String name;
public Test(String name){
this.name = name;
}
a ) Test
b ) test
c ) t
d ) None of the above
//============================================================== Q10:
public class Main {
public static void main(String args[]) {
class Test{
private int x;
public Test(){
this.x = -1;
}
a ) x
b ) 0
c ) -1
d ) None of the above
//============================================================== Q11:
public class Main {
public static void main(String args[]) {
System.out.print(array[0]);
}
}
class Test{
private int x;
public Test(){
this.x = -1;
}
public String toString (){
return "x";
}
}
a ) 0
b ) -1
c ) x
d ) None of the above
//============================================================== Q12:
}
}
class Parent {
private int x;
a ) 1 2
b ) x+" "+y
c ) 1+ +2
d ) None of the above
//============================================================== Q13:
public class Main {
public static void main(String args[]) {
}
}
class Parent {
protected int x;
a ) 1 2
b ) x+" "+y
c ) 1+ +2
d ) None of the above
//============================================================== Q14:
}
}
class Parent {
protected int x;
a ) 3
b ) 1
c ) 1 2
d ) None of the above
//============================================================== Q15:
public class Main {
public static void main(String args[]) {
Child c = new Child(1,2);
Parent P = new Parent(3);
System.out.print(P.x);
}
}
a ) 3
b ) 1
c ) 1 2
d ) None of the above
//============================================================== Q16:
public class Main {
public static void main(String args[]) {
System.out.print(c.x);
}
}
a ) 3
b ) 1
c ) 1 2
d ) None of the above
//============================================================== Q17:
public class Main {
public static void main(String args[]) {
System.out.print(c.print());
}
}
a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q18:
public class Main {
public static void main(String args[]) {
c.print();
}
}
abstract class Parent {
private int x;
a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q19:
public class Main {
public static void main(String args[]) {
Parent p = new Child (1,2);
p.print();
}
}
}
}
a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q21:
p[0].print();
p[1].print();
}
}
class Parent {
private int x;
a ) 11
b ) 24
c ) 13
d ) None of the above
//============================================================== Q22:
public class Main {
public static void main(String args[]) {
Parent [] p = new Parent[2];
p[0] = new Child1(1,2);
p[1] = new Child2(3,4);
p[0].print();
p[1].print();
}
}
a ) 11
b ) 24
c ) 13
d ) None of the above
//============================================================== Q23 :
public class Main {
public static void main(String args[]) {
int x = 1 , y = 0;
try{
if ( y != 0 ) System.out.println( x/y );
}
catch (Exception e) {
System.out.print("1");
}
finally {
System.out.print("2");
}
}
}
a ) 1/0
b ) 2 1
c ) 12
d ) None of the above
//============================================================== Q24 :
public class Main {
public static void main(String args[]) {
String x = "1";
System.out.print(x);
changeTo3(x);
System.out.print(x);
}
a ) 11
b ) 13
c ) 33
d ) None of the above
//============================================================== Q25 :
class MyString{
public String x;
public MyString(){
x = "1";
}
}
public class Main {
public static void main(String args[]) {
MyString string = new MyString();
System.out.print(string.x);
changeTo3(string.x);
System.out.print(string.x);
}
a ) 11
b ) 13
c ) 33
d ) None of the above
//============================================================== Q26 :
public class Main {
public static void main(String args[]) {
int x = 1, y=0;
String name = new String("Jon");
try {
System.out.print(name.length());
System.out.print( x/y );
}
catch (NullPointerException e) {
System.out.print("1");
}
catch (ArithmeticException e) {
System.out.print("2");
}
catch (Exception e){
System.out.print("3");
}
finally{
System.out.print("1");
}
}
}
a ) 21
b ) 1231
c ) 321
d ) 123
e ) None of the above
//============================================================== Q27 :
public class Main {
public static void main(String args[]) {
int x = 1, y=0;
String name = null;
try {
System.out.println(name.length());
System.out.println( y/x );
}
catch (NullPointerException e) {
System.out.print("1");
}
catch (ArithmeticException e) {
System.out.print("2");
}
catch (Exception e){
System.out.print("3");
}
finally {
System.out.print("4");
}
}
}
a ) 134
b ) 14
c ) 1234
d ) 104
e ) None of the above
//============================================================== Q28 :
public class Main {
public static void main(String args[]) {
int x = 1, y = 0, z;
z = x/y;
try {
System.out.println( y/x );
}
catch (NullPointerException e) {
System.out.println("1");
}
catch (ArithmeticException e) {
System.out.println("2");
}
catch (Exception e){
System.out.print("3");
}
finally {
System.out.print("4");
}
System.out.print("5");
}
}
a ) 45
b ) 5
c ) 045
d ) None of the above
//============================================================== Q29 :
public class Main {
public static void main(String args[]) {
try{
fun();
System.out.println("1");
}
catch ( Exception e){
System.out.println( "2" );
}
}
public static void fun() throws Exception{
System.out.println(5/0);
}
}
a ) 12
b ) 21
c ) 2
d ) None of the above
//============================================================== Q30 :
public class Main {
public static void main(String args[]) {
try {
fun();
}
catch (Exception e){
System.out.print("1");
}
}
a ) 2
b ) 1
c ) 12
d ) None of the above
//============================================================== Q31 :
public class Main {
public static void main(String args[]) {
try {
fun();
System.out.print("1");
}
catch (Exception e){
System.out.print("2");
}
System.out.print("3");
}
a ) 23
b ) 123
c ) 2
d ) None of the above
//============================================================== Q32 :
class MyClass{
public int data;
public void doNothing() {
}
}
System.out.print("3");
}
a ) 23
b ) 13
c ) 3
d ) None of the above
//============================================================== Q33 :
interface myInterface{
public void doNothing();
}
class myClass{
public static void doNothing() {
System.out.print("chilling!");
}
}
a ) relaxing
b ) chilling!
c ) chillingrelaxing
d ) None of the above
//============================================================== Q34 :
abstract class myInterface{
abstract public void doNothing();
}
a ) relaxingchilling
b ) relaxing
c ) chillingrelaxing
d ) None of the above
//============================================================== Q35 :
interface MyInterface{
public void doNothing();
}
a ) relaxing
b ) chilling
c ) chillingrelaxing
d ) None of the above
//============================================================== Q36 :
interface MyInterface{
public void doNothing();
}
a ) relaxing
b ) chilling
c ) relaxingchilling
d ) chillingrelaxing
e ) None of the above
//============================================================== Q37 :
class MyClass{
public int data;
public String toString() {
return " "+data;
}
}
try{
for (int i =0; i<3; i++){
array[i].data = i+1;
System.out.print(" "+array[i]);
}
}
catch (Exception e){
System.out.print("BUG");
}
}
}
a ) BUG
b ) 1 2 3
c ) 0 1 2
d ) None of the above
//============================================================== Q38 :
class MyClass{
public int data;
public String toString() {
return " "+data;
}
}
try{
for (int i =0; i<3; i++){
array[i] = myobject;
array[i].data = i+1;
}
for (int i =0; i<3; i++)
System.out.print(" "+array[i]);
}
catch (Exception e){
System.out.print("BUG");
}
}
}
a ) BUG
b ) 1 2 3
c ) 0 1 2
d ) 3 3 3
e ) None of the above
//============================================================== Q39 :
class MyClass {
public int data;
MyClass(){
data = 0;
data++;
}
public String toString() {
return " "+data;
}
}
try {
throw new MyClass();
}
catch (MyClass e){
System.out.println(e);
}
}
}
a ) 0
b ) 1
c ) 2
d ) None of the above
//============================================================== Q40 :
class MyClass extends Exception{
public static int data;
MyClass(){
data++;
}
public String toString() {
return " "+data;
}
}
try {
throw new MyClass();
}
catch (MyClass e){
MyClass obj = new MyClass();
System.out.println(obj);
}
}
}
a ) 1
b ) 2
c ) 3
d ) None of the above
//============================================================== Q41 :
class ChangeClass{
public static void change(String test) {
test = "Test2";
}
}
a ) Test2
b ) Test1
c ) test
d ) None of the above
//============================================================== Q42 :
class ChangeClass{
public static String test;
public static void change() {
test = "Test2";
}
}
a ) Test1
b ) Test2
c ) test
d ) None of the above
//============================================================== Q43 :
public class Main {
public static void main(String args[]) {
Test t = null;
try{
t = new Test();
}
catch ( Exception e){
if ( t != null ) System.out.print(t);
}
System.out.print("Test");
}
class Test{
public Test() throws Exception{
throw new Exception();
}
public String toString (){
return "Test";
}
}
a ) Test
b ) TestTest
c ) TestTestTest
d ) None of the above
//============================================================== Q44 :
public class Main {
public static void main(String args[]) {
Iparent p = new Child ();
Child.x = 2;
p.print();
}
}
interface Iparent {
public int x = 1;
a ) Child 1
b ) Child 2
c ) Child 1Child 2
d ) None of the above
//============================================================== Q45 :
public class Main {
public static void main(String args[]) {
Iparent[] p = new Iparent[2];
try {
p[0] = new Child(2);
p[0].print();
}
catch (Exception e) {
System.out.println("BAD");
}
}
}
interface Iparent {
public int x = 1;
public void print();
}
public Child(int y) {
this.y = y;
}
if ( n == 0) return 0;
return n + fun(n//2);
}
public static void main(String[] args) {
System.out.println(fun(fun(4)));
}
}
a ) 4
b ) 2
c ) 8
d ) None of the above
//============================================================== Q47 :
class Main {
fun(65);
System.out.print("done!");
}
}
a ) -1 is invalid
b ) done!
c ) done-1 is invalid
d ) None of the above