0% found this document useful (0 votes)
49 views

Chapter II.6

The document contains practice problems and answers for a chapter on working with methods and encapsulation in Java. It includes 16 multiple choice problems related to method parameters, return types, access modifiers, pass by value vs reference, and static vs instance methods and fields. The problems test concepts like overriding methods, encapsulation, and how Java passes arguments to methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Chapter II.6

The document contains practice problems and answers for a chapter on working with methods and encapsulation in Java. It includes 16 multiple choice problems related to method parameters, return types, access modifiers, pass by value vs reference, and static vs instance methods and fields. The problems test concepts like overriding methods, encapsulation, and how Java passes arguments to methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

*******************************************************************

* NAILING 1Z0-808:
* PRACTICAL GUIDE TO ORACLE JAVA SE8 PROGRAMMER I CERTIFICATION
* BY IGOR SOUDAKEVITCH
* www.igor.host
*******************************************************************
* Practice questions to
* CHAPTER II.6, Working with Methods and Encapsulation
*******************************************************************

Problem 6.1
Given:
1 package last.vegas;
2
3 class BankAccount {
4 int balance;
5
6 BankAccount(){
7 this.balance = new java.util.Random().nextInt(1000);
8 }
9
10 int getBalance(){
11 return this.balance;
12 }
13 void makeTransfer(int amount){
14 this.balance += amount;
15 }
16 }
17
18 public class HighRoller{
19 public static void main(String[] args) {
20 BankAccount ba = new BankAccount();
21 System.out.println("Was: $" + ba.getBalance());
22 // this.balance = 0;
23 // BankAccount.balance = 0;
24 // new BankAccount(0);
25 // ba.balance = 0;
26 // ba.getBalance() = 0;
27 // ba.makeTransfer(0);
28 // ba.makeTransfer(ba.balance * -1);
29 // ba.makeTransfer(-ba.getBalance());
30 System.out.println("Now: $" + ba.getBalance());
31 }
32 }
Which three lines, when uncommented independently, can empty the bank account
created on line 20?
A. line 22
B. line 23
C. line 24
D. line 25
E. line 26
F. line 27
G. line 28
H. line 29

*******************************************************************
Problem 6.2
Given:
class SpeedBoat {
float speed;
}
public class BoatRace {
public void breakRecord(SpeedBoat sb, float speed) {
speed = speed * 2;
sb.speed = sb.speed + speed;
}
public static void main(String[] args) {
SpeedBoat sb = new SpeedBoat();
sb.speed = 200;
float currentRecord = 400;
BoatRace br = new BoatRace();
br.breakRecord(sb, currentRecord);
System.out.println(sb.speed + " vs " + currentRecord);
}
}
What is the result?
A. 200.0 vs 400.0
B. 600.0 vs 800.0
C. 1000.0 vs 400.0
D. Compilation fails

*******************************************************************

Problem 6.3
Given:
class WhichOne {
public static void run(int x, int y) {
System.out.println("working with int: " + (x + y));
}
public static void run(Integer x, Integer y) {
System.out.println("working with Integer: " + (x + y));
}
public static void run(float x, float y) {
System.out.println("working with float: " + (x + y));
}
public static void run(double x, double y) {
System.out.println("working with double: " + (x + y));
}
public static void main(String[] args) {
run(1, 2);
run(1.0, 2.0);
}
}
What is the result?
A. working with int: 3
working with float: 3.0
B. working with int: 3
working with double: 3.0
C. working with Integer: 3
working with double: 3.0
D. working with Integer: 3
working with float: 3.0
*******************************************************************

Problem 6.4
Given:
public class Doubler {
public static void main(String[] args) {
int a = 100;
System.out.print(doubleValue(a));
System.out.print(" : " + a);
}
static int doubleValue(int a){
a = a * 2;
return a;
}
}
What is the result?
A. 200 : 100
B. 100 : 100
C. 200 : 200
D. Compilation fails

*******************************************************************

Problem 6.5
Suppose, a method is declared to take two arguments but the caller passes to it
only one parameter. What is the result?
A. Compilation fails.
B. The missing argument is given the default value for its declared type.
C. An IllegalArgumentException is thrown at run time.
D. A NoSuchMethodException is thrown at run time.

*******************************************************************

Problem 6.6
Given:
class Test {
public static void modify(int[] arr) {
for (int idx = 0; idx < arr.length; idx++) {
arr[idx] = arr[idx] + 1;
System.out.print(arr[idx] + " ");
}
}
public static void main (String[] args) {
int[] arr = {10, 20, 30};
for (int e : arr) { System.out.print(e + " "); }
modify(arr);
for (int e : arr) { System.out.print(e + " "); }
}
}
What is the result?
A. 10 20 30 11 21 31 10 20 30
B. 10 20 30 11 21 31 11 21 31
C. Compilation fails
D. A RuntimeException is thrown

*******************************************************************

Problem 6.7
Given:
class PassByValue {
private int a = 0;
private int b = 0;
public void run1(int a, int b) {
a = a;
b = b;
System.out.println("a = " + this.a + ", b = " + this.b);
}
public void run2(int a, int b) {
this.a = a;
this.b = b;
System.out.println("a = " + this.a + ", b = " + this.b);
}
public static void main(String[] args) {
PassByValue pbv = new PassByValue();
pbv.run1(1, 2);
pbv.run2(3, 4);
}
}
What is the result?
A. a = 0, b = 0
a = 0, b = 0
B. a = 1, b = 2
a = 3, b = 4
C. a = 0, b = 0
a = 3, b = 4
D. a = 1, b = 2
a = 0, b = 0

*******************************************************************

Problem 6.8
Given:
class OverloadTester {
static double run(double a) {
System.out.print("double: ");
return a;
}
static int run(double a) {
System.out.print("int: ");
return (int)a;
}
static String run(double a) {
System.out.print("String: ");
return new String("" + a);
}
public static void main(String[] args) {
System.out.println(run(1.0));
}
}
What is the result?
A. double: 1.0
B. int: 1
C. String: 1.0
D. Compilation fails

*******************************************************************

Problem 6.9
Which two are valid in the body of a method that does not return a value?
A. Omission of the return statement
B. return null;
C. return void;
D. return;

*******************************************************************

Problem 6.10
Which is the valid declaration of a method that takes two arguments of the type int
and returns the result of their division?
A. divide (int a, int b) { return a/b; }
B. double divide (int a, b) { return a/b; }
C. int divide (int a, int b) { return a/b; }
D. double divide (int a, int b) { a/b; }
E. void divide (int a, int b) { return a/b; }
F. None of the above

*******************************************************************

Problem 6.11
Given:
class Invalid {
private static double value;
public static int getValue(){ return value; };
public static void Main(String[] args) {
System.out.println(getValue());
}
}
This class fails compilation. What is the reason?
A. There is no corresponding setValue() method
B. The method getValue() returns an incompatible data type
C. A static field cannot be declared private
D. The field’s value is uninitialized
E. The main() method has wrong signature

*******************************************************************

Problem 6.12
Given:
class Test {
static int[] a = {1, 2, 3, 4};
int[] b = {5, 6, 7, 8};
public static void main(String[] args) {
Test t1 = new Test(), t2 = new Test();
t1.a[3] = 10;
t1.b[3] = 20;
t2.a[3] = 30;
t2.b[3] = 40;
System.out.println (
t1.a[3] + " " + t1.b[3] + " " +
t2.a[3] + " " + t2.b[3]);
}
}
What is the result?
A. 10 20 30 40
B. 30 20 30 40
C. A run-time exception is thrown
D. Compilation fails

*******************************************************************

Problem 6.13
Given two classes:
package pack1;
public class A {
public int a = 1;
protected int b = 2;
int c = 3;
private int d = 4;
}

1 package pack2;
2 import pack1.*;
3 public class B extends A {
4 public static void main(String[] args) {
5 A obj = new B();
6 System.out.println("a = " + obj.a);
7 System.out.println("b = " + obj.b);
8 System.out.println("c = " + obj.c);
9 System.out.println("d = " + obj.d);
10 }
11 }
How many LOCs in the class B fail compilation?
A. None
B. One
C. Two
D. Three
E. Four

*******************************************************************

Problem 6.14
What is the name of the Java concept that uses access modifiers to protect variables
defined by a class?
A. Abstraction
B. Encapsulation
C. Inheritance
D. Instantiation
E. Polymorphism

*******************************************************************

Problem 6.15
Given:
class A {
int a;
static int b;
A(int input) { if (b <= input) b = a = input; }
void show() { System.out.println("a = " + a + ", b = " + b); }
}
public class Test {
public static void main(String[] args) {
A obj1 = new A(20);
A obj2 = new A(60);
A obj3 = new A(40);
obj1.show();
obj2.show();
obj3.show();
}
}
What is the result?
A. a = 20, b = 60
a = 60, b = 60
a = 40, b = 60
B. a = 20, b = 60
a = 60, b = 60
a = 0, b = 60
C. a = 20, b = 20
a = 60, b = 60
a = 40, b = 40
D. a = 20, b = 20
a = 60, b = 60
a = 0, b = 60

*******************************************************************

Problem 6.16
Given two classes:
public class WaterConsumer {
WaterConsumer wcons = new WaterConsumer();
public void useWater(double m3){
wcons.updateBill(m3);
}
}
public class UtilityAccount {
private double m3;
private double rate = 1.23;
private double bill;
//line X
}
Which code block, when inserted at line X, will implement the following
requirements?
1. The code should successfully update the water consumption bill (represented
by the variable bill), which is defined as the number of cubic meters of water
consumed by the customer (the variable m3) multiplied by the cost of water per
cubic meter (the variable rate);
2. The code should also protect the variable bill from tampering such as an
attempt to decrease its value by an instance of the class WaterConsumer.

A. public void updateBill(double m3) {


this.m3 += m3;
this.bill = this.m3 * this.rate;
}
B. public void updateBill(double m3) {
if (m3 > 0){
this.m3 += m3;
this.bill = this.m3 * this.rate;
}
}
C. private void updateBill(double m3) {
if (m3 > 0) {
this.m3 += m3;
this.bill = this.m3 * this.rate;
}
}
D. public void updateBill(double m3) {
if(m3 > 0) {
this.m3 += m3;
calcBill(this.m3);
}
}
public void calcBill(double m3) {
bill = m3 * rate;
}

*******************************************************************

Problem 6.17
Given:
class Production {
static int count = 0;
int a = 0;
public void rampUp() {
while (a < 10) {
a++;
count++;
}
}
public static void main(String[] args) {
Production p1 = new Production();
Production p2 = new Production();
p1.rampUp();
p2.rampUp();
System.out.print("p1.count = " + p1.count + ", p2.count = " + p2.count);
}
}
What is the result?
A. p1.count = 20, p2.count = 20
B. p1.count = 10, p2.count = 10
C. p1.count = 10, p2.count = 20
D. Compilation fails

*******************************************************************

Problem 6.18
Given:
package base;
public class Base {
public void run(){ System.out.print("No args! "); };
protected void run(int arg){ System.out.print(arg + " "); };
void run(double arg){ System.out.print(arg + " "); };
private void run(String arg){ System.out.print("\"" + arg + "\""); };
}

package derived;
import base.Base;
public class Derived extends Base {
public static void main(String[] args) {
Base obj = new Derived();
obj.run(); // line 1
obj.run(1); // line 2
obj.run(1.0); // line 3
obj.run("1"); // line 4
}
}
What is the result?
A. No args! 1 1.0 "1"
B. Compilation fails on line 4
C. Compilation fails on lines 4 and 3
D. Compilation fails on lines 4, 3 and 2
E. Compilation fails on lines 4, 3, 2 and 1

*******************************************************************

Problem 6.19
Given the following class:
1 public class A {
2 public double a;
3 public static void main (String[] args) {
4 A obj = new A();
5 a = 10;
6 }
7 }
Which two options, when used independently, will make this code compile and run?
A. Replace line 2 with public int a;
B. Replace line 2 with static int a;
C. Replace line 4 with static int a;
D. Replace line 4 with static double a;
E. Replace line 5 with a = 10.0d;
F. Replace line 5 with new A().a = 10;
*******************************************************************

Problem 6.20
Given:
1 class StaticTest {
2 static void run1() {
3 run2();
4 run3();
5 StaticTest.run2();
6 }
7 static void run2() {
8 run1();
9 StaticTest.run3();
10 }
11 void run3() {
12 run1();
13 }
14 }
Which two LOCs fail compilation?
A. line 3
B. line 4
C. line 5
D. line 8
E. line 9
F. line 12

*******************************************************************

Problem 6.21
Given the contents of the file U.java:
1 abstract class A{}
2 final class F{}
3 public class U{}
4 protected class O{}
5 private class R{}
6 static class S{}
Which three LOCs fail compilation?
A. line 1
B. line 2
C. line 3
D. line 4
E. line 5
F. line 6

*******************************************************************

Problem 6.22
Given:
class Test {
static int a = 42;
public static void main(String[] args) {
Test t = new Test();
t.a++;
Test.a++;
t.a--;
System.out.println(--t.a + " " + Test.a);
}
}
What is the result?
A. 42 42
B. 40 41
C. 41 40
D. 43 44

*******************************************************************

Problem 6.23
Given the following two classes:
class A {
String str;
A() {}
private String run() {
return this.str += "!";
}
}
class B extends A {
B(String str) {
this.str = str;
}
private void doStuff() {
System.out.println(run());
}
public static void main (String [] args) {
new B("Hello").doStuff();
}
}
What should be done to make the code compile?
A. Make the class A public
B. Declare the method run() as protected instead of private
C. Make the A() constructor public
D. Make the A() constructor protected
E. Make the method doStuff() package-private

*******************************************************************

Problem 6.24
Given the following code fragment:
class ExamTaker {
private String name;
private int score;
public ExamTaker(String name) {
this(); //line E1
setName(name);
}
public ExamTaker(String name, int score) {
ExamTaker(name); //line E2
setScore(score);
}
//setter and getter methods go here
@Override
public String toString() {
return getName() + " " + getScore() + " ";
}
}
class Results{
public static void main(String[] args) {
ExamTaker e1 = new ExamTaker("Alice");
ExamTaker e2 = new ExamTaker("Bob",62);
System.out.print(e1);
System.out.println(e2);
}
}
What is the result?
A. Alice 0 Bob 62
B. Compilation fails only on line E1
C. Compilation fails only on line E2
D. Compilation fails at both line E1 and line E2

*******************************************************************

Problem 6.25
Given:
class Greetings {
String str = "Hello!";
Greetings(String str) {
this.str = str;
}
void greet() {
System.out.println(str);
}
public static void main(String[] args) {
new Greetings("Hi!").greet();
}
}
What is the result?
A. Hi!
B. Hello!
C. No output
D. Compilation fails
E. A RuntimeException is thrown

*******************************************************************

Problem 6.26
Given:
class A { }
class B { B() {} }
class C { C(String str ) {} }
Which class has a default constructor?
A. Only A
B. Only B
C. Only C
D. A and B
E. B and C
F. A and C
G. All three of them

*******************************************************************

Problem 6.27
Given the contents of four source code files:
File E.java:
package packOne;
class A { }
public class E {
private E() { }
}
File Parent.java:
package packTwo;
final class B{};
File D.java:
package packThree;
public abstract class D {
private void D() {}
}
File Child.java:
package packFour;
import packOne.*;
import packTwo.*;
import packThree.*;
// line C1
Which class definition, when inserted at line C1, will enable the code to compile?
A. class C extends A { }
B. class C extends B { }
C. class C extends D { }
D. class C extends E { }
E. None of the above

*******************************************************************

Problem 6.28
Given two classes:
class Exams {
String name;
int year;
List exams = new ArrayList();
// line XXX
this.name = name;
this.year = year;
this.exams = exams;
}
public String toString() {
return name + " in " + year + ": " + exams;
}
}
public class Scheduler {
public static void main (String[] args) {
List al = new ArrayList();
al.add("1Z0-808");
al.add("1Z0-809");
Exams e = new Exams("Bob", 2016, al);
System.out.println(e);
}
}
Which option, when inserted at line XXX, will enable the class Scheduler to print
Bob in 2016: [1Z0-808, 1Z0-809]?
A. private Exams(String name, int year, List exams){
B. public void Exams(String name, int year, List exams){
C. Exams(String name, int year, List exams){
D. Exams(String name, int year, ArrayList exams){

*******************************************************************

Problem 6.29
Given:
package parent;
public class Parent {
Parent() { System.out.println("Hello"); }
}

package child;
import parent.Parent;
public class Child extends Parent {
private Child() { }
public static void main (String[] args) {
new Child();
}
}
Which modification will make the code compile and print Hello?
A. Change the Parent() constructor to Parent(Child child)
B. Make the Child() constructor public instead of private
C. Add the modifier protected to the Parent() constructor
D. Remove the Child() constructor
E. Remove the modifier private from the Child() constructor

*******************************************************************

Problem 6.30
Which statement is true about the default constructor?
A. It can take arguments.
B. It has implicit public access modifier in its declaration.
C. It can be added by the programmer.
D. The default constructor of a subclass always invokes the no-arg constructor
of its superclass.

*******************************************************************

Problem 6.31
Given:
class Parent{
int a;
public Parent(int a) {
this.a = a;
}
}
class Child extends Parent{
int b;
public Child(int a, int b) {
super(a);
this.b = b;
}
// line X
}
Which two LOCs can be added independently at line X?
A. Child() {}
B. Child(int a) { this.b = a; }
C. Child(int b) { this.a = b; }
D. Child(int b) { super(Math.PI > Math.E ? 3 : 2); }
E. Child(int c) { this(c = 1, c = 2 ); }

*******************************************************************

Problem 6.32
Given:
class Hamlet {
static String word = "";
Hamlet(String word){ this.word += word; }
{ word += "a"; }
static { word += "i"; }
{ word += "o"; }
}
class TypingMonkey {
public static void main(String[] args) {
System.out.print(Hamlet.word + " ");
new Hamlet("u");
System.out.print(Hamlet.word + " ");
new Hamlet("e");
System.out.print(Hamlet.word + " ");
}
}
What is the result?
A. i iaou iaouaoe
B. i iiaou iiaouaoe
C. i iaou iaouiaoe
D. i aiou aioue

*******************************************************************

Problem 6.33
Given:
class Test{
static int a;
int b;
Test(int a, int b) {
this.a += a;
this.b += b;
}
void modify(int a, int b){
this(a, b);
}
public static void main(String[] args) {
Test t1 = new Test(10, 20);
Test t2 = new Test(30, 40);
t1.modify(10, 20);
t2.modify(10, 20);
System.out.println("a = " + Test.a + ", b = " + t2.b);
}
}
What is the result?
A. a = 40, b = 40
B. a = 40, b = 60
C. a = 60, b = 40
D. Compilation fails

*******************************************************************

Problem 6.34
The Real World Scenario sidebar on page 208 in the OCA Study Guide by Jeanne
Boyarsky and Scott Selikoff discusses the so-called defensive copy and provides
this example:
public Mutable(StringBuilder b) {
builder = new StringBuilder(b);
}
public StringBuilder getBuilder() {
return new StringBuilder(builder); // XXX
}
Now, the Constructor Summary for the class StringBuilder says that the class
defines four constructors none of which lists a StringBuilder object as its
argument.

Hence the question: Is line XXX valid?


A. Hell, no! it should’ve been return new StringBuilder(builder.toString());
B. Oh yes, most definitely; after all, the authors of any study guide written to
the masses should know what they are talking about…

*******************************************************************

Problem 6.35
Given:
class A {
static { System.out.print("Hello from"); }
static String name = " A";
}
class B extends A{
static { System.out.print(" B"); }
}
class C extends B {
static { System.out.print(" C"); }
}
class Test {
public static void main(String[] args){
System.out.println(C.name);
}
}
What is the result?
A. A
B. Hello from A
C. Hello from C A
D. Hello from B C A

*******************************************************************

Problem 6.36
Given:
public class Test{
public void testRefs(String str, StringBuilder sb){
str = str + sb.toString();
sb.append(str);
str = null;
sb = null;
}
public static void main(String[] args){
String str = "aaa";
StringBuilder sb = new StringBuilder("bbb");
new Test().testRefs(str, sb);
System.out.println("str=" + str + " sb=" + sb);
}
}
What is the result?
A. str=null sb=null
B. str=aaa sb=null
C. str=aaa sb=bbb
D. str=aaa sb=aaabbb
E. str=aaa sb=bbbaaabbb
F. Compilation fails
G. A NullPointerException is thrown at runtime

You might also like