Log in
Sign up for FREE
arrow_back
Library
Writing Classes Test Multiple Choice Part 1-60%
By Mickey Arnold
star
star
star
star
star
Share
share
Last updated almost 2 years ago
40 questions
Add this activity
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Question 1
1.
What is output by the code below?
public class CS
{
public void one()
{
System.out.print("one");
two(true);
}
private void two(boolean stop)
{
System.out.print("two");
if(!stop)
one();
}
}
///////////////////////////////////////////////////////////
//code in the main of another class
CS test = new CS();
test.one();
one
onetwoone
onetwoonetwo
onetwo
There is no output due to a syntax error.
Question 2
2.
What is output by the code below?
public class CS
{
public void one()
{
System.out.print("one");
two(true);
}
public void two(boolean stop)
{
System.out.print("two");
if(!stop)
one();
}
}
///////////////////////////////////////////////////////////
//code in the main of another class
CS test = new CS();
test.two(false);
twoone
twoonetwoone
two
twoonetwo
onetwooneoneone
Question 3
3.
What is output by the code below?
public class CS
{
public void one()
{
System.out.print("one");
two(true);
}
public void two(boolean stop)
{
System.out.print("two");
if(!stop)
one();
}
}
///////////////////////////////////////////////////////////
//code in the main of another class
CS test = new CS();
test.one();
one
onetwooneoneone
onetwoone
onetwoonetwo
onetwo
Question 4
4.
What is output by the code below?
public class CS
{
int q = 0;
public void go(int i)
{
if(i==0)
q++;
for(int j = 0;j<i;j++)
go2(i-1);
}
public void go2(int i)
{
if(i==0)
q++;
for(int j = 0;j<i;j++)
go(i-1);
}
public int getQ()
{
return q;
}
}
///////////////////////////////////////////////////////////
//code in the main of another class
CS test = new CS();
test.go(2);
System.out.println( test.getQ() );
1
2
3
4
5
Question 5
5.
What is output by the code below?
class CS
{
int q = 0;
public void go(int i)
{
if(i==0)
q++;
for(int j = 0;j<i;j++)
q+=2;
}
public int getQ()
{
return q;
}
}
///////////////////////////////////////////////////////////
//code in the main of another class
CS test = new CS();
test.go(11);
System.out.println( test.getQ() );
30
24
16
26
22
Question 6
6.
What is output by the code below?
public class CS
{
int q = 0;
public void go(int i)
{
if(i==0)
q++;
for(int j = 0;j<i;j++)
go2(i-1);
}
public void go2(int i)
{
if(i==0)
q++;
for(int j = 0;j<i;j++)
go(i-1);
}
public int getQ()
{
return q;
}
}
///////////////////////////////////////////////////////////
//code in the main of another class
CS test = new CS();
test.go(4);
System.out.println( test.getQ() );
16
24
36
28
42
Question 7
7.
Consider the class and statements that follow.
public class Dog
{
private String voice;
public void setVoice( String v )
{
voice = v;
}
public void barkOnce()
{
System.out.print( voice );
}
public void barkTwice()
{
barkOnce();
barkOnce();
}
}
Given the declaration below, which of the following are valid statements?
Dog d = new Dog();
d.setVoice( "roof" );
I.
d.barkOnce();
II.
d.barkTwice("rrrrrr");
III.
System.out.println( d.barkTwice() );
I only
II only
III only
I and II only
none of these statements are valid
Question 8
8.
Consider the class and statements that follow.
public class Dog
{
private String voice;
public void setVoice( String v )
{
voice = v;
}
public void barkOnce()
{
System.out.print( voice );
}
public void barkTwice()
{
barkOnce();
barkOnce();
}
}
Given the declaration below, which of the following are valid statements?
Dog d = new Dog();
d.setVoice( "roof" );
I.
d.barkOnce();
II.
d.barkTwice();
III.
System.out.println( d.barkTwice() );
I only
II only
III only
I and II only
none of these statements are valid
Question 9
9.
Consider the class and statements that follow.
public class Dog
{
private String voice;
public void setVoice( String v )
{
voice = v;
}
public void barkOnce()
{
System.out.print( voice );
}
public void barkTwice()
{
barkOnce();
barkOnce();
}
}
Given the declaration below, which of the following are valid statements?
Object d = new Dog();
I.
d.barkOnce();
II.
d.barkTwice("rrrrrr");
III.
System.out.println( d.barkTwice() );
I only
II only
III only
I and II only
none of these statements are valid
Question 10
10.
Consider the class and statements that follow.
public class Dog
{
private String voice;
public void setVoice( String v )
{
voice = v;
}
private void barkOnce()
{
System.out.print( voice );
}
public void barkTwice()
{
barkOnce();
barkOnce();
}
public String toString()
{
return voice;
}
}
Given the declaration below, which of the following are valid statements?
Object d = new Dog();
((Dog)d).setVoice( "roof" );
I.
d.barkOnce();
II.
((Dog)d).barkTwice();
III.
System.out.println( d );
I only
II only
I and III only
II and III only
none of these statements are valid
Question 11
11.
Consider the class and client code below.
public class Aplus
{
public double go( double a ){
return a * 3;
}
public double fun( double b ){
return go( b ) + b;
}
}
//client code in another class
Aplus x = new Aplus();
System.out.println( x.go(2) );
8.0
12.0
3.0
9.0
6.0
Question 12
12.
Consider the class and client code below.
public class Aplus
{
public double go( double a ){
return a * 3;
}
public double fun( double b ){
return go( b ) + b;
}
}
//client code in another class
Aplus x = new Aplus();
System.out.println( x.go(3) );
8.0
9.0
12.0
6.0
3.0
Question 13
13.
Consider the class and client code below.
public class Aplus
{
public double go( double a )
{
return a * 3;
}
public double fun( double b )
{
return go( b ) + b;
}
}
//client code in another class
Aplus x = new Aplus();
System.out.println( x.fun(3) );
3.0
12.0
6.0
9.0
8.0
Question 14
14.
Consider the class and statements that follow.
public class Money
{
private double amount;
public Money(double am)
{
amount = am;
}
public void addQuarters(int q)
{
amount += q * 0.25;
}
public void addDimes(int d)
{
amount += d * 0.1;
}
public double getAmount()
{
return amount;
}
}
What is the output of the code below?
Money m = new Money(.3);
m.addQuarters(3);
m.addDimes(6);
m.addQuarters(4);
System.out.println(m.getAmount());
13.3
2.45
2.65
3.25
2.35
Question 15
15.
Consider the class and statements that follow.
public class Money
{
private double amount;
public Money(double am)
{
amount = am;
}
public void addQuarters(int q)
{
amount += q * 0.25;
}
public void addDimes(int d)
{
amount += d * 0.1;
}
public double getAmount()
{
return amount;
}
}
What is the output of the code below?
Money m = new Money(2.1);
m.addDimes(7);
m.addQuarters(7);
m.addDimes(2);
System.out.println(m.getAmount());
2.65
18.1
4.75
5.05
2.1
Question 16
16.
What is output by the code below?
public class Check
{
private int fun;
public void change()
{
int fun = 5;
}
public String toString()
{
return "" + fun;
}
}
//client code
Check test = new Check();
test.change();
out.println(test);
0
5
6
7
There is no output due to a syntax error.
Question 17
17.
What is output by the code below?
public class Check
{
private int fun;
public void change()
{
fun = 5;
int fun = 6;
}
public String toString()
{
return "" + fun;
}
}
//client code
Check test = new Check();
test.change();
out.println(test);
0
5
6
7
There is no output due to a syntax error.
Question 18
18.
What is output by the code below?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
total = one + two;
}
public String toString()
{
return "" + total;
}
}
//code in the main of another class
Check test = new Check();
test.setNums(3,-6);
test.add();
out.println(test);
0
3
2
9
-3
Question 19
19.
What is output by the code below?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
total = one + two;
two = 5;
}
public String toString()
{
total = 2;
return "" + total;
}
}
//code in the main of another class
Check test = new Check();
test.setNums(-6,3);
test.add();
out.println(test);
-1
-3
5
4
2
Question 20
20.
What is output by the code below?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
one = 5;
total = one + two;
two = 5;
}
public String toString()
{
return "" + total;
}
}
//code in the main of another class
Check test = new Check();
test.setNums(3,2);
test.add();
out.println(test);
10
7
5
8
2
Question 21
21.
How many modifier methods are there in class Check?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
one = 5;
total = one + two;
two = 5;
}
public String toString()
{
return "" + total;
}
}
2
3
4
1
0
Question 22
22.
How many instance variables are there in class It?
public class It
{
private int val;
}
3
4
1
0
2
Question 23
23.
How many methods are there in class It?
public class It
{
private int val;
public It()
{
val = 7;
}
public int getIt()
{
return val;
}
}
2
0
4
3
1
Question 24
24.
How many methods are there in class It?
public class It
{
private int val;
public It()
{
val = 7;
}
public int getIt()
{
return val;
}
}
0
4
3
1
2
Question 25
25.
How many accessor methods are there in class It?
public class It
{
private int val;
public It()
{
val = 7;
}
public int getIt()
{
return val;
}
}
2
0
3
4
1
Question 26
26.
What is output by the code below?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
total = one + two;
}
public int getTotal()
{
return total;
}
}
//code in the main of another class
Check test = new Check();
test.setNums(3,-6);
test.add();
out.println(test.getTotal());
-3
2
0
3
9
Question 27
27.
How many modifier / mutator methods are there in class Check?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
total = one + two;
}
public int getTotal()
{
return total;
}
}
2
3
1
0
4
Question 28
28.
How many instance variables are there in class Check?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
total = one + two;
}
public int getTotal()
{
return total;
}
}
0
4
3
1
2
Question 29
29.
How many constructors are there in class Check?
public class Check
{
private int one, two, total;
public void setNums(int n1, int n2)
{
one = n1;
two = n2;
}
public void add()
{
total = one + two;
}
public int getTotal()
{
return total;
}
}
2
3
1
4
0
Question 30
30.
Consider the class and client code below.
public class Aplus
{
public double go( double a )
{
return a * 3;
}
public double fun( double b )
{
return b * 2;
}
}
//client code in another class
Aplus x = new Aplus();
System.out.println( x.fun(6) );
12.0
8.0
9.0
60
3.0
Question 31
31.
What is output by the code below?
public static void aMethod(int a){
a=9;
a=7;
}
//test code
int aParam = 4;
aMethod(aParam);
out.println(aParam);
9
3
7
0
4
Question 32
32.
What is output by the code below?
public static void bMethod(int b){
b=9;
out.print(b+" ");
b=7;
}
//test code
int bParam = 4;
bMethod(bParam);
out.println(bParam);
9 7
4 4
7 4
9 9
9 4
Question 33
33.
What is output by the code below?
public static void cMethod(int c){
c=9;
c=7;
out.print(c+" ");
}
//test code
int cParam = 4;
cMethod(cParam);
out.println(cParam);
7 4
4 4
9 9
9 7
9 4
Question 34
34.
What is output by the code below?
public static void addStuff(int d, int c)
{
System.out.println(c + " + " + d + " = " + (c+d));
}
///////////////////////////////////////////////////////////////////////////////////////
//client code in the main of a runner class
int a = 32;
int b = 9;
addStuff(a,b);
c + d = c+d
41
32 + 9 = 41
9 + 32 = 41
c + d = 41
Question 35
35.
What is output by the code below?
public static void withdraw(double balance, double amount)
{
balance -= amount;
}
///////////////////////////////////////////////////////////////////////////////////////
//client code in the main of a runner class
double balance = 1020.37;
double amount = 350;
withdraw(balance, amount);
System.out.println(balance);
350
670.37
balance
1370.37
1020.37
Question 36
36.
What is output by the following code?
public static void test(Integer x, int y)
{
x += y;
}
///////////////////////////////////////////////////////////////////////////////////////
//client code in the main of a runner class
Integer i = new Integer(4);
test(i,5);
System.out.println(i);
0
4
9
there is no output due to a syntax error
there is no output due to a runtime error
Question 37
37.
What is output by the code below?
public class A
{
private int one;
public A(int o){
one=o;
}
public void setOne(int o){
one = o;
}
public int getOne(){
return one;
}
}
public class B
{
public void changeOne(A param){
param=new A(0);
param.setOne(7);
}
public void changeTwo(A param){
param.setOne(4);
}
public void changeThree(A param){
param.setOne(9);
param=new A(0);
}
}
//code in the main of another class
B test = new B();
A theA = new A(2);
test.changeOne(theA);
out.println(theA.getOne());
2
9
0
7
4
Question 38
38.
What is output by the code below?
public class A
{
private int one;
public A(int o){
one=o;
}
public void setOne(int o){
one = o;
}
public int getOne(){
return one;
}
}
public class B
{
public void changeOne(A param){
param=new A(0);
param.setOne(7);
}
public void changeTwo(A param){
param.setOne(4);
}
public void changeThree(A param){
param.setOne(9);
param=new A(0);
}
}
//code in the main of another class
B test = new B();
A theA = new A(2);
test.changeTwo(theA);
System.out.println(theA.getOne());
2
0
7
9
4
Question 39
39.
What is output by the code below?
public class A
{
private int one;
public A(int o){
one=o;
}
public void setOne(int o){
one = o;
}
public int getOne(){
return one;
}
}
public class B
{
public void changeOne(A param){
param=new A(0);
param.setOne(7);
}
public void changeTwo(A param){
param.setOne(4);
}
public void changeThree(A param){
param.setOne(9);
param=new A(0);
}
}
//code in the main of another class
B test = new B();
A theA = new A(2);
test.changeThree(theA);
System.out.println(theA.getOne());
2
0
7
9
4
Question 40
40.
What is output by the code below?
String a = "aplus";
String b = a;
b = null;
System.out.println( a );
null
apluscomp
This is no output due to a runtime error.
aplus
comp