Professional Documents
Culture Documents
DUMP GOLD
Question 1
What will the following code print?
public class Test{
public int luckyNumber (int seed){
if (seed> 10) return seed %10;
int x = 0;
try {
if (seed%2==0) throw new Exception(No Even no);
else return x;
}
catch (Exception e){
return 3;
}
finally{
return 7;
}
}
public static void main (String args[]){
int amount = 100, seed = 6;
switch(new Test().luckyNumber(6)){
case 3: amount == amount * 2;
case 7: amount == amount * 2;
case 6: amount == amount * 2;
default:
}
System.out.println(amount);
}
}
A: 400
Question 2
Given
public class TestStr {
public static void main(String[]args){
String str 4 +2 A;
System.out.print(Str);
}
}
What is the output?
A: Compilation fail
Question 3
Given the code fragment:
int[] intArray = new int [] {1,2,3,4,5,6,7,8};
for (int =0 ; I <3 ; i++){
testfor(intArray);
}
private static final void testFor(int [] int_array){
int total= 0;
for (int i= int_array){
total+=i;
}
}
What is the result?
A: the code runs with no output
Question 4
class EJavaGuruExcep2 {
public static void main(String args[]) {
EJavaGuruExcep2 var = new EJavaGuruExcep2();
var.printArrValues (args);
}
void printArrValues(String[] arr) {
try {
System.out.println(arr[0] + ":" + arr[1]);
}catch (NullPointerException e) {
System.out.println("NullPointerException");
}catch (IndexOutOfBoundsException e) {
System.out.println("IndexOutOfBoundsException");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException");
}
}
}
What is the result?
A: The code will fail to compile
Question 5
Given:
public class Circle {
double radius;
public double area;
public Circle (double r) { radius = r;}
Question 6
class A{
int a = 1;
public void methodA(){
System.out.print("A ");
}
}
class B extends A
{
int a = 2;
int b = 1;
public void methodB(){
System.out.print("B ");
}
}
class C extends B{
int a = 3;
int b = 2;
int c = 1;
public void methodA(){
System.out.print("C ");
}
}
class Test{ public static void main(String[] args) {
A a = new C();
a.methodA();
C c = (C) a;
c.methodB();
c.methodA();
}
}
What is the result?
A.- A B A B.- A B C C.- C B A D.- C B C E.- compilations fails F.- An exception is thrown at run time
Question 7
Given:
public class Basic{ private static int letter; public static int getLetter(); public static void main
(String[] args){ System.out.println(getLetter());}} Why will the code not compile?
A: The getLetter method has no body
Question 8
Which two statements are benefits of encapsulation?
A: allows a class implementation to change without changing the clients
B: protects confidential data from leaking out of the objects
Question 9
class Shirt {
12. private int shirtID = 0;
13. private String description = "-description required-";
14. private char colorCode = 'U';
15. private double price = 0.0;
16. public char getColorCode() {
17. return colorCode;
18. }
19. private void setColorCode(char newCode) {
20. colorCode = newCode;
21. }
22.
23. }
24. class ShirtTest {
25. public static void main (String[] args) {
26. Shirt theShirt = new Shirt();
27. char colorCode;
28. theShirt.setColorCode('R');
29. colorCode = theShirt.getColorCode();
30. System.out.println("Color Code: " + colorCode);
31. theShirt.setColorCode('G');
32. colorCode = theShirt.getColorCode();
33. System.out.println("Color Code: " + colorCode);
34. }
35. }
Question 10
Public static void main (String [] args){
String str = 420;
Str+=42;
System.out.print(str);
}
What is the result?
A.- 42042
Question 11
Given the code fragment:
Outer: for (int i = 0;i<3; i++){
for (int j= 0 ; j<2 ; j ++){
System.out.println(hello);
continue outer;
}
System.out.println(outer);
}
System.out.println(Good bye) ;
What is the result?
A: Hello
A: Hello
A: Hello
A: GoodBye
Question 12
Class A{
public void methodA(){
System.out.print(A);
}
Class B extends A {
public void methodB(){
System.out.print(B);
}
}
Class C extends B{
public void methodC(){
System.out.print(C);
}
}
Class Test{
public static void main (String[] args){
A[] a= {new A(), new B(),new C()};
for (A a1:a){
a1.methodC();
}
}
A: Compilation fails
Question 13
public class Average{
public int averageGrade() {
int totalGrade=0;
int i = 0;
String results = "resultado";
for (; i < results.length(); i++);
{
results = results.substring(i);
totalGrade += results.charAt(i);
}
if (i>0)
return totalGrade/i;
else return 0;
}
public static void main(String [] args){
int x = averageGrade();
System.out.println(x);
}
}
A: Compilation fails
Question 14
class Test {
public static void method (int [] array){
array[0]= 7;
array[1]= 7;
}
public static void main (String []args){
int [] array = {1,2};
method(array);
System.out.println(array[1]);
}
}
A: 7
Question 15
Given:
public class Basic{
private static int letter;
public static int getLetter();
public static void Main (String[] args){
System.out.println(getLetter());
Why will the code not compile?
A: The getLetter() method has no body
Question 16
class Test{
public static void main(String[] args) {
int = 100_345_123
int j = 123;
System.out.println(i-j);
}
}
What is the result?
A: 100345000
Question 17
public static void main(String[] args) {
if ((isItSmall(6)) & (isItSmall(7))) {
System.out.println("Result is true");
}
if ((isItSmall(6)) | (isItSmall(9))) {
System.out.println("Result is true");
}
}
public static boolean isItSmall(int i) {
if (i < 5) {
System.out.println("i < 5");
return true;
} else {
System.out.println("i >= 5");
return false;
}
}
What is the result?
A:
B:
C:
D:
compilation fails
i >= 5 i >= 5 i >= 5 i >= 5
i >= 5 i >= 5 i >= 5
i <= 5 i >= 5 i >= 5
Question 18
11.class Elevator {
12.
private boolean doorOpen;
13.
private int current Floor;
14.
private final int TOP_FLOOR;
15.
private final int MIN_FLOOR;
16.
private Elevator (){
17
doorOpen = false;
18.
currentFloor = 1;
19.
TOP_FLOOR = 10;
20.
MIN_FLOOR =1;
21.
}
22.
private void goUp(){
23.
If (currentFloor == TOP_FLOOR){
24.
System.out.printnln(Cannot go up further!);
25.
}
26.
if (currentFloor<TOP_FLOOR){
27.
currentFloor++;
28.
System.out.println(Floor:+currentFloor);
29.
}
30.
}
31.
32.class Test{
33.
public static void main (String[] args){
34.
Elevator theElevator = new Elevator();
35.
theElevator.currentFloor = 15;
36.
}
37.}
What is the result?
A.- Compilation fails because of an error in the lines 34 and 35.
Question 19
interface Data{ public void load(); }
abstract class Info { public abstract void load(); }
Which class correctly uses the Data interface and Info class?
Question 20
What is wrong with the following code written in single file named TestClass java?
Class SomeThrowable extends Throwable{}
Class MyThrowable extends SomeThrowable{}
public class TestClass{
public static void main(String args[]) throws SomeThrowable{
try{
m1();
}catch(SomeThrowable e){
throw e;
} finally{
System.out.println(Done);
}
}
public static void m1()throws MyThrowable {
throw new MyThrowable();
}
}
A: Done will be printed followed of an exepction
Question 21
public class ScopeTest { int z; public static void main(String[] args) { ScopeTest myScope = new
ScopeTest(); int z = 6; System.out.println(z); myScope.doStuff(); System.out.println(z);
System.out.println(myScope.z); } void doStuff() { int z = 5; doStuff2(); System.out.println(z); } void
doStuff2() { z = 4; } } What is the result?
A: 6564
Question 22
11.
12.
13.
14.
15.
16.
17.
18.
class Shirt {
private int shirtID = 0;
private String description = "-description required-";
private char colorCode = 'U';
private double price = 0.0;
public char getColorCode() {
return colorCode;
}
19.
20.
21.
22.
23.
Question 23
Given.
class BreakContinue {
public static void main( String [] args ) {
for( int i = 0 ; i < 10 ; i++ ) {
if( i % 2 == 0) {
continue;
}
System.out.println("The number is " + i );
if( i == 7 ) {
break;
}
}
}
}
What is the result when this program is executed?
A:
The number is 1
The number is 3
The number is 5
The number is 7
Question 24
Given:
10. interface A { void x(); }
11. class B implements A { public void x() {} public void y() {} }
12. class C extends B { public void x() {} }
And:
20. java.util.List<A> list = new java.util.ArrayList<A>();
Question 25
Given:
interface A {public void aMethod();}
interface B {public void bMethod();}
interface C extends A B {public void cMethod();}
class D implements B {
public void bMethod(){}
}
class E extends D implements C {
public void aMethod(){
public void bMethod(){
public void cMethod(){
}
What is the result?
A: If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined
in line 9
Question 26
Which of the following options contain correct code to declare and initialize variables to store
whole numbers?(Choose three)
A.- long 3a = 0x10C;
B.- short $4 = 0512;
C.- byte _7 = -0;
D.- long #8 = 123456789L;
E.- float a = 3.14;
F.- int bi= 0b100;
G.- char c = "C";
Question 27
Considering the following program, which of the options are true?
public class FinallyTest{
public static void main(String args[]){
try{
if (args.length == 0) return;
else throw new Exception("Some Exception");
}
catch(Exception e){
System.out.println("Exception in Main");
}
finally{
System.out.println("The end");
}
}
}
A.- If run with no arguments, the program will only print 'The end'.
C.- If run with one argument, the program will print 'Exception in Main' and 'The end'.
Question 28
class A{
public void doA(int k) throws Exception { //0
for (int = 0; i<10; i++){
if (i==k) throw new Exception(index of k is+i);//1
}
}
public void doB(Boolean f) { //2
if(f) {
doA(15); // 3
}
else return;
}
public static void main(String[] args) { // 4
A a = new A();
a.doB(args.length>0); // 5
}
}
Which of the following statement is correct?
D.- This will compile if throws Exception is added at line //2 as well as //4
Question 29
Given:
public class ScopeTest {
int j, k;
public static void main(String[] args) { doStuff(); }
static void doStuff() { int x = 5; doStuff2(); System.out.print("x"); }
static void doStuff2() { int y = 7; System.out.print("y"); for (int z = 0; z < 5; z++) {
System.out.print("z");
System.out.print("y");
}
}}
Question 30
Consider the following program:
class ChainedException {
public static void foo() {
try {
throw new ArrayIndexOutOfBoundsException();
} catch(ArrayIndexOutOfBoundsException oob) {
RuntimeException re = new RuntimeException(oob);
re.initCause(oob);
throw re;
}
}
public static void main(String []args) {
try {
foo();
} catch(Exception re) {
System.out.println(re.getClass());
}
}
}
When executed, this program prints which of the following?
B: class java.lang.IllegalStateException
Question 31
class A{
int a = 1;
public void methodA(){
System.out.print("A ");
}
}
class B extends A{
int a = 2;
int b = 1;
public void methodA(){
System.out.print("B ");
}
}
class C extends A{
int a = 3;
int b = 2;
int c = 1;
public void methodA(){
System.out.print("C ");
}
}
class Test{
public static void main(String[] args) {
A a = new B();
C c = (C)a;
c.methodA();
}
}
What is the result?
E.- An exception is thrown at run time
Question 32
public class SampleClass{
public static void main (String[]args){
AnotherSampleClass asc = new AnotherSampleClass();
SampleClass sc = new SampleClass();
// TODO code application logic here
}
}
class AnotherSampleClass extends SampleClass {
}
Which statement, when inserted into line// TODO code application logic here *, is valid change?
D: sc= asc;
Question 33
class A{
int a = 1;
Question 34
Class Test{
public static void main (String[] args){
Int a= 5;
Int b = 6;
Int c= 7;
a = b;
b= c;
c= a;
a= 8;
b= 9;
System.out.println(c);
}
}
A: 6
Question 35
Select the incorrect statement:
A.- code that handles all the checked exceptions can still throw unchecked exception
B.- exceptions handling speeds up execution of the code
C.-exception handling is used to define code that should execute when a piece of code
throws an exception
D.-exception enable a developer to define the programming logic separate from exception
handling code
Question 36
10. abstract public class Employee{
11.
protected abstract double getSalesAmount();
12.
public double getCommision(){
13.
return getSalesAmount() * 0.15;
14.
}
15.}
16. class Sales extends Employee{
17
// INSERT METHOD HERE
18.}
Which two methods , inserted independently at line 17 correctly complete the Sales class?
(Choose two)
B.- public double getSalesAmount() { return1230.45;}
D.- protected double getSalesAmount() { return1230.45;}
Question 37
Which staments are true (Choose four)
A.- methods cant be defined as overloaded methods if they differ only their return types or
access modifiers
B.-the argument lists can differ by changes in the number of parameters that are accepted
C.- the argument lists can differ by changes in the positions of parameters that are accepted
E.- the argument lists can differ by changes in the types of parameters that are accepted
Question 38
public static void main(String[] args) {
if ((isItSmall(6)) & (isItSmall(7))) {
System.out.println("Result is true");
}
if ((isItSmall(6)) | (isItSmall(9))) {
System.out.println("Result is true");
}
}
public static boolean isItSmall(int i) {
if (i < 5) {
System.out.println("i < 5");
return true;
} else {
System.out.println("i >= 5");
return false;
}
}
What is the result?
A:
B:
C:
D:
compilation fails
i >= 5 i >= 5 i >= 5 i >= 5
i >= 5 i >= 5 i >= 5
i <= 5 i >= 5 i >= 5
Question 39
What is the output of the following code?
class EJava4 {
void foo(){
try{
String s = null;
System.out.println(1);
try{
System.out.println(s.length());
}
catch (NullPointerException e){
System.out.println(inner);
}
System.out.println(2);
}
catch (NullPointerException e){
System.out.println(outter);
}
}
public static void main(String[ ] args){
EJava4 obj = new EJava4();
Incomplete but correct
A:
1
inner
2
Question 40
Given a code fragment:
StringBuilder sb = new StringBuilder();
String h1 = Hello world,
sb.append(hello).append(World);
if (h1==sb.toString()) {
System.out.printl(They match);
}
if (sb.equals(h1)) {
System.out.println(They really match);
}
What is the result?
A: Nothing is printed to the screen
Question 41
What is the default constructor for the following class?
public class Penny{
String name = lane;
}
A: public Penny(){}
Question 42
Given the following code public class simple {/*line 1*/public float price ; /*line 2*/public static void
main (String [] args){ /*line 3*/ Simple price = new Simple();/*line 4*/ price = 4 /*line 5*/}/*line
6*/}/*line 7*/
What will make this code to compile?
A: change line 5 to the following price.price = 4;
Question 43
Class Test{
public static void main(String[] args){
boolean string = false;
If (string^false)
System.out.println(string= true);
else if(string)
System.out.println(string);
}
}
What is the resul?
A: no output
Question 44
Given the code fragment:
boolean isTrue = true;
outer: for (int i = 0; i < 5; i++) {
while (isTrue) {
System.out.println("Hello");
break outer;
}
System.out.println("Outer loop");
}
System.out.println("Good Bye")
What is the result?
A.Hello
Outer loop
Good Bye
Question 45
class Test {
public static void method (int [] array){
array[0]= 7;
}
public static void main (String [] args){
int [] array = {1,2};
method(array);
System.out.println(array[1]);
}
}
A: 2
Question 46
Select the incorrect statements (Choose two):
A.- java.lang.Throwable is the base class of all type of exceptions.
C.- Error is an unchecked exception.
Question 47
Given:
public class Basic (public static void main (String[]args) {
float x= 32.00f%3.00f;
System.out.println(x);
}
}
What is the result?
A: 2.0
Question 48
Select the correct option(Choose three):
A.- You cannot handle runtime exceptions.
B.- If a method throws a checked exception, it must be either handled by the method or specified in
its throws clause.
C.- If a method throws a runtime exception, it may include the exception in its throws clause.
D.- Runtime exceptions are checked exceptions.
Question 49
class SuperTest {
public static void main(String[] args) {
statement1
statement2
statement3
}
}
class Shape {
public Shape() {
System.out.println("Shape: constructor");
}
public void foo() {
System.out.println("Shape: foo");
}
}
class Square extends Shape {
public Square() {
super();
}
public Square(String label) {
System.out.println("Square: constructor");
}
public void foo() {
super.foo();
}
public void foo(String label) {
System.out.println("Square: foo");
}
}
What should statement1, statement2, and statement3, be respectively, in order to produce the
result?
A:
Square square = new Square ();
square.foo (bar);
square.foo();
Question 50
public static void main (String[] args) {
String s = new String(5);
System.out.println(1+10+s+1+10);
}
What is the result?
A: 115110
Question 51
public class Hello{
String title;
int value;
public hello(){
title +=world;
}
public hello(int value ){
this();
this.value = value;
title = hello;
}
}
And
Hello c= new hello(5);
System.out.println(c.title);
Question 52
Which of the following methods correctly accepts three whole numbers as method arguments and
returns their sum as a decimal number?
B:
public double subtractNumbers(long arg1, int arg2, int arg3) {
double sum = arg1 + arg2 + arg3;
return sum;
}
C:
public double numbers(long arg1, int arg2, double arg3) {
return arg1 + arg2 + arg3;
}
D:
public float wakaWakaAfrica(long a1, long a2, int a977) {
double sum = a1 + a2 + a977;
return (float)sum;
}
Question 53
Examine the following code and select the correct options (Choose two)
class Ejava {
public Ejava(){
this(7);
System.out.println(public);
}
private EJava(int val){
this(sunday);
System.out.println(private);
}
protected EJava(String val){
System.out.println(protected);
}
}
class TestEJava{
public static void main (String[] args) {
EJava ejava = new EJava();
}
A:
The class Ejava defines three overloaded constructors
The code prints the following:
Protected
Private
Public
Question 54
class Test{
public static int method(int x){
return x [3];
}
public static void main(String[] args){
int [] a = new int [] {1,2,3,4 };
method(a);
}
}
A: no output
Question 55
What two changes can you do, independent of each other, to make the following code compile:
//assume appropriate imports
class PortConnector {
public PortConnector(int port) {
if (Math.random() > 0.5) {
throw new IOException();
}
throw new RuntimeException();
}
}
public class TestClass {
public static void main(String[] args)
{
try{
PortConnector pc = new PortConnector(10);
} catch (RuntimeException re) {
re.printStackTrace(); } } }
A.- add throws IOException to the main method.
Question 56
class Overloading{
void x(int i) {(System.out.println(one):}
void x(String s){ system.out.println(two);}
void x(double d) {System.out.println(three);}
public static void main (String[] args){
new Overloading().x(4.0);
}
}
What is the result?
A: three
Question 57
class A{
int a = 1;
public void methodA(){
System.out.print("A ");
}
}
class B extends A{
int a = 2;
int b = 1;
public void methodB(){ System.out.print("B ");
}
}
class C extends B{
int a = 3;
int b = 2;
int c = 1;
public void methodA(){
System.out.print("C ");
}
}
class Test{
public static void main(String[] args) {
A a = new C();
a.methodA();
C c = (C) a;
c.methodB();
c.methodA();
}
}
What is the result?
A: C B C
Question 58
class Elevator {
private boolean doorOpen;
private int current Floor;
private final int TOP_FLOOR;
private final int MIN_FLOOR;
private Elevator (){
doorOpen = false;
currentFloor = 1;
TOP_FLOOR = 10;
MIN_FLOOR =1;
}
private void goUp(){
if (currentFloor == TOP_FLOOR){
System.out.printnln(Cannot go up further!);
}
if (currentFloor<TOP_FLOOR){
currentFloor++;
System.out.println(Floor:+currentFloor);
}
}
}
A: Compilation succeeds
Question 59
class Elevator {
private boolean doorOpen;
private int current Floor;
private final int TOP_FLOOR;
private final int MIN_FLOOR;
private Elevator (){
doorOpen = false;
currentFloor = 1;
TOP_FLOOR = 10;
MIN_FLOOR =1;
}
private void goUp(){
if (currentFloor == TOP_FLOOR){
System.out.printnln(Cannot go up further!);
}
if (currentFloor<TOP_FLOOR){
currentFloor++;
System.out.println(Floor:+currentFloor);
}
}
}
public static void main (String[] args){
Elevator theElevator = new Elevator();
theElevator.currentFloor = 15;
}
}
A: No output
Question 60
Which declaration initializes a boolean variable?
A.- boolean m = true;
B.- boolean m = null;
C.- boolean k = 0;
D.- boolean m = null;
E.- boolean h = 1;
Question 60
Given the following code:
public class simple {/*line 1*/
public float price ; /*line 2*/
public static void main (String [] args){ /*line 3*/
Simple price = new Simple();/*line 4*/
price = 4 /*line 5*/
}/*line 6*/
}/*line 7*/
What will make this code to compile?
Question 61
public static void main (String [] args){
String str = 420;
Str+=42;
System.out.print(str);
}
What is the result?
A: 42042
Question 61
Given the following declaration:
public class SomeClass{
public int i;
public static void main(String argv [ ]){
SomeClass sc = new SomeClass();
// Comment line
}
}
Which of the following statements are correct if they replace the comment line?
B.- System.out.println(sc.i);
D.- System.out.println((new SomeClass()).i);
Question 61
Given the following declaration:
public class SomeClass{
public int i;
public static void main(String argv [ ]){
SomeClass sc = new SomeClass();
// Comment line
}
}
Which of the following statements are correct if they replace the comment line?
B.- System.out.println(sc.i);
D.- System.out.println((new SomeClass()).i);
Question 62
Given:
public class ScopeTest {
int j, k;
public static void main(String[] args) { doStuff(); }
static void doStuff() { int x = 5; doStuff2(); System.out.print("x"); }
static void doStuff2() { int y = 7; System.out.print("y"); for (int z = 0; z < 5; z++) {
System.out.print("z");
System.out.print("y");
}
}}
What is the result?
A: yzyzyzyzyzyx
Question 63
What will be the output when the following program is run?
package exceptions;
public class TestClass {
public static void main(String[] args) {
try{
doTest();
}
catch(MyException me){
System.out.println(me);
}
}
static void doTest() throws MyException{
int[] array = new int[10];
array[10] = 1000;
doAnotherTest();
}
static void doAnotherTest() throws MyException{
throw new MyException("Exception from doAnotherTest");
}
}
class MyException extends Exception {
public MyException(String msg){
super(msg);
}
}
A:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 10
at exceptions.TestClass.doTest(TestClass.java:24)
at exceptions.TestClass.main(TestClass.java:14)
Question 64
Given:
public class ScopeTest {
int j, k;
public static void main(String[] args) { doStuff(); }
static void doStuff() { int x = 5; doStuff2(); System.out.print("x"); }
static void doStuff2() { int y = 7; System.out.print("y"); for (int z = 0; z < 5; z++) {
System.out.print("z");
System.out.print("y");
}
}}
What is the result?
A: yzyzyzyzyzyx
Question 65
Given the code fragment:
System.out.println(1+2+3+4+5);
What is the result?
A: 3345
Question 66
Given the code fragment:
outer: for (int i = 0;i<3; i++){
for (int j= 0 ; j<2 ; j ++){
System.out.println(hello);
continue outer;
}
System.out.println(outer);
}
System.out.println(Good bye) ;
What is the result?
A:
Hello
Hello
Hello
Good bye
Question 65
class Course {
String courseName;
Course() {
Course c = new Course();
c.courseName = "Oracle";
}
}
class EJavaGuruPrivate2 {
public static void main(String args[]) {
Course c = new Course();
c.courseName = "Java";
System.out.println(c.courseName);
}
}
}
A: The code will throw an exception or an error at runtime