Log in
Sign up for FREE
arrow_back
Library

Writing Classes Test Multiple Choice Part 1-60%

star
star
star
star
star
Last updated almost 2 years ago
40 questions
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.

Question 2
2.

Question 3
3.

Question 4
4.

Question 5
5.

Question 6
6.

Question 7
7.

Question 8
8.

Question 9
9.

Question 10
10.

Question 11
11.

Question 12
12.

Question 13
13.

Question 14
14.

Question 15
15.

Question 16
16.

Question 17
17.

Question 18
18.

Question 19
19.

Question 20
20.

Question 21
21.

Question 22
22.

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

Question 23
23.

Question 24
24.

Question 25
25.

Question 26
26.

Question 27
27.

Question 28
28.

Question 29
29.

Question 30
30.

Question 31
31.

Question 32
32.

Question 33
33.

Question 34
34.

Question 35
35.

Question 36
36.

Question 37
37.

Question 38
38.

Question 39
39.

Question 40
40.

What is output by the code below?

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

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();
There is no output due to a syntax error.
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);
onetwooneoneone
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();
onetwo
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() );
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() );
22
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() );
42
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() );
none of these statements are valid
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() );
none of these statements are valid
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() );
none of these statements are valid
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 );
none of these statements are valid
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) );
9.0
6.0
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) );
6.0
3.0
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) );
9.0
8.0
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());
3.25
2.35
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());
5.05
2.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);
There is no output due to a syntax error.
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);
There is no output due to a syntax error.
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);
-3
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);
2
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);
2
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;
}
}
0
4
1
0
2
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;
}
}
2
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());
9
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;
}
}
4
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;
}
}
2
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;
}
}
0
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) );
3.0
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);
4
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 4
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);
9 4
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);
9 + 32 = 41
c + d = 41
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);
1020.37
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);
there is no output due to a runtime error
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());
4
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());
4
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());
4
null
apluscomp
This is no output due to a runtime error.
aplus
comp