Writing Classes Test Multiple Choice Part 1-60%

Last updated over 1 year ago
40 questions
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();

1

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);

1

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();

1

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

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() );

1

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() );

1

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() );

1

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() );

1

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() );

1

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 );

1

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) );

1

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) );

1

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) );

1

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());

1

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());

1

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);

1

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);

1

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);

1

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

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);

1

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;
}
}

1

How many instance variables are there in class It?
public class It
{
private int val;
}

1

How many methods are there in class It?
public class It
{
private int val;
public It()
{
val = 7;
}
public int getIt()
{
return val;
}
}

1

How many methods are there in class It?
public class It
{
private int val;
public It()
{
val = 7;
}
public int getIt()
{
return val;
}
}

1

How many accessor methods are there in class It?
public class It
{
private int val;
public It()
{
val = 7;
}
public int getIt()
{
return val;
}
}

1

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());

1

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;
}
}

1

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;
}
}

1

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;
}
}

1

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) );

1

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);

1

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);

1

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);

1

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);

1

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);

1

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);

1

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());

1

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());

1

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());

1

What is output by the code below?

String a = "aplus";
String b = a;
b = null;
System.out.println( a );