Computer Science: Application Multiple Choice

Question: 1

Consider the following methods involving strings and integers.

public void update(String x, int y)

{

x = x + “Retriever”;

y = y * 3;

}

public void myTest()

{

String s = “Golden”;

int num = 7;

update(s, num);

/*end of method*/

}

When a call to myTest() is invoked, what are the values of s and num at the point indicated in by /end of method/?

s is the string “Golden”; num = 7
s is the string “GoldenRetriever”; n = 7
s is the string “Golden”; n = 21
s is the string “GoldenRetriever”; n = 21
s is the string “Retriever”; n = 21

Question: 2

The following method

public String removeFromString(String old, String frag)

removes all occurrences of the string frag from the string old. For example,

removeFromString(“hello there!”, “he”) returns “llo tre!”

removeFromString(“lalalala”, “l”) returns “aaaa”

Which of the following blocks of code will successfully implement removeFromString()?

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + frag.length());

old = old.substring(0, index);

index = old.indexOf(frag);

}

}

return old;

}

private String removeFromString(String old, String frag)

{

int i = old.indexOf(frag);

while (i > -1) {

String rest = old.substring(i + frag.length());

old = old.substring(0, i);

old = old + rest;

i = old.indexOf(frag);

}

return old;

}

private String removeFromString(String old, String frag)

{

int i = old.indexOf(frag);

while (i > -1) {

String rest = old.substring(i + frag.length());

old = old.substring(0, i+1);

old = old + rest;

i = old.indexOf(frag);

}

return old;

}

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + frag.length());

old = old.substring(0, index + 1);

index = old.indexOf(frag);

}

}

return old;

}

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + 1 + frag.length());

old = old.substring(0, index + 1);

index = old.indexOf(frag);

}

}

return old;

}

Question: 3

Which of the following will execute without throwing an exception error?

String str1 = null;

String str2 = “”;

if (str1.equals(str2))

System.out.println(“empty strings?”);

String str1 = “Golden”;

String str2 = “Retriever”;

if (str1.equals(str2))

System.out.println(“Golden Retriever!”);

String str1 = “Golden”;

String str2 = str1.substring(4);

System.out.println(str1 + str2);

None of the above
B and C above.

Question: 4

What is an instance method?

An instance method is piece of code called on a specific instance of the class. It is called with a receiver object.
An instance method is a piece of code that does not depend on any specific instances, just on the general class.
An instance method adds functionality to a class by creating private fields.
An instance method adds functionality to the class by printing out a result.

Question: 5

Refer to the Card and Deck classes shown below.

public class Card

{

private string suit;

private int value; //13 values for each suit in deck (0 to 12)

public Card (String cardSuit, int cardValue)

{ /* implementation */}

public String getSuit()

{return suit;}

public int getValue()

{return value;}

public String toString()

{

String faceValue = “”;

if (value >= 2 && value <=10)

return value + “ of “ + suit;

if (value == 11)

faceValue = “Jack”;

else if (value == 12)

faceValue = “Queen”;

else if (value == 0)

faceValue = “King”;

else if (value == 1)

faceValue = “Ace”;

else

return faceValue + “ of “ + suit;

}

}

public class Deck

{

private Card[] deck;

public final static int NUMCARDS = 52;

public Deck()

{ /* implementation */}

/** Shuffle deck. */

public void shuffle()

{ /* implementation */}

//Other methods not shown.

}

Consider a displayDeck method that is added to the Deck class.

/** Show the cards in the deck so they display one per line. */

public void displayDeck()

{

/* implementation */

}

Which of the following is correct /* implementation */ code?

I.

System.out.println(deck);

II.

for (card card : deck)

System.out.println((String) card);

III.

for (card card : deck)

System.out.println(card);

I only
II only
III only
I and II only
I and III only

Question: 6

Which of these is an example of calling a static method?

point.setX(x)
Math.abs(x)
student.getName()
square(x)

Question: 7

Which variables are in scope at the point labeled // POINT A? In other words, which variables exist at that point in the code?

public class ScopeQuiz extends ConsoleProgram

{

private int count = 0;

public void run()

{

int sum = 0;

// POINT A

for(int i = 0; i < 10; i++)

{

sum += i;

}

int result = sum + count;

}

}

Only sum
sum and count
sum, count, and i
sum and result
sum, count, i, and result

Question: 8

What is printed by the following program?

public class PrintQuestion extends ConsoleProgram

{

private String phrase = “Hello World!”;

public void run()

{

String phrase = “hi”;

System.out.println(phrase);

phrase = “hello”;

}

}

Hello World!
hi
hello
Nothing, this program will not run because it is not possible to have two variables with the same name.

Question: 9

What is the this keyword?

The this keyword refers to a convention in Java for talking about classes.
The this keyword refers to the current class name.
The this keyword refers to the current instance of the class.
The this keyword is a special keyword that can only be used in class methods.

Question: 10

Given the following Circle class

public class Circle

{

private int radius;

public Circle(int theRadius)

{

radius = theRadius;

}

public void setRadius(int newRadius)

{

radius = newRadius;

}

public boolean equals(Circle other)

{

return radius == other.radius;

}

}

What is the output of the following code snippet?

Circle one = new Circle(10);

Circle two = new Circle(10);

System.out.println(one == two);

true
false

Question: 11

Given the following Circle class

public class Circle

{

private int radius;

public Circle(int theRadius)

{

radius = theRadius;

}

public void setRadius(int newRadius)

{

radius = newRadius;

}

public boolean equals(Circle other)

{

return radius == other.radius;

}

}

What is the output of the following code snippet?

public void run()

{

Circle one = new Circle(5);

Circle two = new Circle(10);

foo(two);

System.out.println(one.equals(two));

}

public void foo(Circle x)

{

x.setRadius(5);

}

true
false

Question: 1

Given this code snippet,

public class Person

{

public String name;

public Person(String name)

{

this.name = name;

}

public void changeName(String name)

{

this.name = name;

}

public void printName()

{

System.out.println(this.name);

}

}

what will this code print?

Person myPerson = new Person(“Bob”);

myPerson.changeName(“Joe”);

myPerson.name = “John”;

myPerson.printName();

Bob
Joe
John
This code will error
Bob

Joe

John

CHECK

Question: 2

Given this code snippet,

public class Athlete

{

public Athlete(String name)

{

this.name = name;

}

}

what is missing from the class definition?

Need to declare the name instance variable
Missing void in constructor definition
Class constructors must be private
Java does not use the keyword this, instead it uses self
No constructor is defined for the class

CHECK

Question: 3

Given the code snippet,

public class Pokemon

{

private String name;

private int health;

public Pokemon(String name, int health)

{

name = name;

health = health;

}

}

what is wrong with the class definition?

Hint : We expect the instance variables name and health to be initialized in the constructor.

Missing void in constructor definition
Class constructors must be private
The constructor parameters cannot have the same names as the instance variables.
Must use this in constructor when the constructor parameters have the same name as instance variables. ie:

this.name = name;

this.health = health;

CHECK

Question: 4

What is the output running Main.bar();?

public class Main

{

private static int n = 0;

public static void bar()

{

Main m1 = new Main();

Main m2 = new Main();

Main m3 = new Main();

m1.foo();

}

public Main()

{

n = n + 1;

}

public void foo()

{

System.out.println(n);

}

}

0
1
2
3

CHECK

Question: 5

Refer to this code snippet.

public class Main

{

private static int n = 0;

public static void bar()

{

Main m1 = new Main();

Main m2 = new Main();

Main m3 = new Main();

m1.foo();

}

public Main()

{

n = n + 1;

}

public void foo()

{

System.out.println(n);

}

}

Suppose the following method is added:

public void setN(int newValue)

{

n = newValue;

}

What would be the output of the program if bar() were changed to the following:

public static void bar()

{

Main m1 = new Main();

Main m2 = new Main();

Main m3 = new Main();

m1.setN(5);

m3.foo();

}

0
1
3
5

CHECK

Question: 6

What is the output of the following program?

public class Main

{

private String str = “bar”;

public void run()

{

Main m = new Main(“foo”);

System.out.println(m.getString());

}

public Main(String str)

{

str = str;

}

public String getString()

{

return str;

}

}

The program does not compile.
The program does not produce any output.
foo
bar

CHECK

Question: 7

Given an instance of the Athlete class called athlete, what is the proper way to set the value of the jersey number after it has been instantiated?

public class Athlete

{

private String first_name;

private String last_name;

private int jersey;

public int getJersey()

{

return this.jersey;

}

public Athlete(String first_name, String last_name, int jersey)

{

this.first_name = first_name;

this.last_name = last_name;

this.jersey = jersey;

}

}

athlete.jersey = 23;
athlete.getJersey() = 23;
athlete.getJersey(23);
athlete.setJersey(23);
You cannot set the jersey, since jersey is private and there is no setter method.

CHECK

Question: 8

Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number?

public class Athlete

{

private String first_name;

private String last_name;

private int jersey;

public int getJersey()

{

return this.jersey;

}

public Athlete(String first_name, String last_name, int jersey)

{

this.first_name = first_name;

this.last_name = last_name;

this.jersey = jersey;

}

}

athlete.jersey
athlete.jersey()
athlete.getJersey
athlete.getJersey()
None of the above

CHECK

Question: 9

Given this Athlete class, which of the following setter methods for the jersey variable is correct?

public class Athlete

{

private String first_name;

private String last_name;

private int jersey;

public int getJersey()

{

return this.jersey;

}

public Athlete(String first_name, String last_name, int jersey)

{

this.first_name = first_name;

this.last_name = last_name;

this.jersey = jersey;

}

}

public int setJersey(int jersey)

{

this.jersey = jersey;

}

public void setJersey(int jersey)

{

this.jersey = jersey;

}

public void setJersey(int jersey)

{

jersey = jersey;

}

public void setJersey(jersey)

{

this.jersey = jersey;

}

public void setJersey(int jersey)

{

this.jersey = jersey;

return jersey;

}

CHECK

Question: 10

Mark the valid way to create an instance of Athlete given the following Athlete class definition:

public class Athlete {

String first_name;

String last_name;

int jersey;

public Athlete(String first_name, String last_name, int jersey) {

this.first_name = first_name;

this.last_name = last_name;

this.jersey = jersey;

}

}

athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
Athlete athlete = new Athlete(“Dirk” “Nowitzki” 41);
Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, “41”);

CHECK

Question: 11

Mark the valid way to create an instance of Foo given the following code:

public class Foo {

int bar;

String stoo;

public Foo() {

this.bar = 0;

this.stoo = “”;

}

public Foo(int bar) {

this.bar = bar;

stoo = “You.”;

}

}

Foo fee = Foo(32);
Foo fee;

new fee = Foo();

Foo fee = new Foo(10, “You.”)
Foo fee;

fee = new Foo();

CHECK

Question: 12

Given the following definition for the class Athlete:

public class Athlete

{

String first_name;

String last_name;

int jersey;

public Athlete(String first_name, String last_name)

{

this.first_name = first_name;

this.last_name = last_name;

this.jersey = 0;

}

public Athlete(String first_name, String last_name, int jersey)

{

this.first_name = first_name;

this.last_name = last_name;

this.jersey = jersey;

}

}

Which of the following are valid instantiations of the class Athlete?  I – Athlete joe = new Athlete(“Joe”, “Montana”);  II – Athlete joe = new Athlete(“Joe, “Montana”, “16”);  III – Athlete joe = new Athlete(“Joe”, “Montana”, 16);

I and III
I only
II only
I II and III
II and III

CHECK

Question: 13

Which methods of class Foo can be called without an actual instance of the class Foo?

public class Foo

{

public static void foo()

{

};

public void bar()

{

};

public void baz()

{

};

}

All methods require an instance of Foo in order to be called.
No methods require an instance of Foo in order to be called.
bar()
baz()
foo()

CHECK

Question: 14

Given this code snippet,

public class Person

{

public String name;

public Person(String name)

{

System.out.println(name);

this.name = name;

}

public void printName()

{

System.out.println(this.name);

}

}

public class Miner extends Person

{

public Miner(String name)

{

super(name);

}

}

what will this program print?

Person somePerson = new Person(“Bob”);

Miner mikeTheMiner = new Miner(“Mike”);

mikeTheMiner.printName();

Bob
Mike

Mike

Bob

Bob

Mike

Mike

This code will error
Bob

Mike

CHECK

Question: 15

The Java Classes Skeleton, Spider, and Zombie all extend the Java Class Monster. The Monster Class is defined below.

public class Monster

{

public String name;

public String type;

private int x;

private int y;

public Monster(String name, String type)

{

this.name = name;

this.type = type;

this.x = 0;

this.y = 0;

}

public void move(x, y)

{

this.x = x;

this.y = y;

}

}

What variables and methods do the Skeleton, Spider, and Zombie Classes inherit?

Everything
Only the variables name and type
Only the variables
Only the methods
The variables name and type and the method move

CHECK

Question: 16

Given the definitions of class A and class B

class A

{

public int i;

public int j;

public A()

{

i = 1;

j = 2;

}

}

class B extends A

{

int a;

public B()

{

super();

}

}

what is the output of this code

B obj = new B();

System.out.println(obj.i + ” ” + obj.j);

1 2
2 1
Runtime Error
Compilation Error

CHECK

Question: 17

Given the class definitions of Vector2D and Vector3D below:

public class Vector2D {

public int x;

public int y;

public Vector2D() {}

public Vector2D(int x,int y) {

this.x = x;

this.y = y;

}

// other code

}

public class Vector3D extends Vector2D

{

public int z;

// other code

}

Which of the constructors that follow would be valid to put in the Vector3D class?

Possible constructors for Vector3D:  I. public Vector3D() {}  II. public Vector3D(int x, int y, int z)  {  super(x,y);  this.z = z;  }  III. public Vector3D(int x, int y)  {  this.x = x;  this.y = y;  this.z = 0;  }

I, II, and III
I and II
II and III
Only III
Only II

CHECK

Question: 18

Based on this code snippet

public class Shape

{

public String getShapeName()

{

return “Shape”;

}

}

public class Rectangle extends Shape

{

public String getShapeName()

{

return “Rectangle”;

}

}

public class Square extends Rectangle {}

public class Oval extends Shape

{

public String getShapeName() {

return “Oval”;

}

}

public class Circle extends Oval

{

public String getShapeName()

{

return “Circle”;

}

}

what does this program output?

Shape shape1 = new Shape();

Shape shape2 = new Rectangle();

Shape shape3 = new Square();

Shape shape4 = new Circle();

System.out.println(shape1.getShapeName());

System.out.println(shape2.getShapeName());

System.out.println(shape3.getShapeName());

System.out.println(shape4.getShapeName());

Shape Rectangle Rectangle Oval
Shape Rectangle Square Circle
This code will error
Shape Rectangle Rectangle Circle
Shape Shape Shape Shape

CHECK

Question: 19

You want to create a class called FavoriteMovies that inherits from Movies. Movies has public instance variables, private methods, and a constructor. Which of the options below will NOT be inherited from Movies in a FavoriteMovies class?

· I. The public instance variables in Movies.

· II. The Movies constructor.

· III. The private methods of Movies.

I only
II only
III only
I and II only
II and III only

CHECK