Computer Science: Application Multiple Choice

Computer Science: Application Multiple Choice

HIGHLIGHT THE CORRECT ANSWER
1. Why do we use methods in Java programming?

Break down our program into smaller parts
Avoid repeating code
Make our program more readable
All of the above

2. What are parameters?

The value that a method returns.
The values that a method prints to the screen.
The formal names given to the data that gets passed into a method.
The type that is given to a variable.

3. What is a return value?

The value that a method prints to the screen.
The value that is inputted to a method.
The value that a user inputs to a program.
The value that a method outputs.

4. What is a Javadoc?

The reference guide to Java that is in the back of a textbook.
A program that cures the bugs in your Java program.
Data that gets passed into a method
A comment that clearly states the functionality and usage of a class or method.

5. Does the following code snippet have a run time error, compile time error, both, or neither?
int x = 3
int y = 4;
int sum = x + y;

Run time error
Compile time error
Both
Neither

6. Which exception will be thrown as a result of running this code snippet?
String hello = “hello”;
char last = hello.charAt(10);

IndexOutOfBoundsException
ArithmeticException
IllegalArgumentException
None

7. What are parameters?

The value that a method returns.
The values that a method prints to the screen.
The formal names given to the data that gets passed into a method.
The type that is given to a variable.

8. What is a method?

A procedure that is defined by the user.
A keyword used to loop for a fixed amount of time.
A place where we can store data.
The type that is given to a variable.

9. What is a Javadoc?

The reference guide to Java that is in the back of a textbook.
A program that cures the bugs in your Java program.
Data that gets passed into a method
A comment that clearly states the functionality and usage of a class or method.

10. What is a return value?

The value that a method prints to the screen.
The value that is inputted to a method.
The value that a user inputs to a program.
The value that a method outputs.

11. What does void mean?

Void means that a method returns any value.
Void means that a method takes no parameters.
Void means that a method returns no value.
Void means that a method can take any parameter.

12. What does this method call output?
public double doubleOrNothing(double myDouble)
{
return (double) (int) myDouble * 2;
}
doubleOrNothing(9.9);

20
18.8
18.0
This method is improperly written.

13. What does this method call return?
public int doubleInt(int x)
{
x * 2;
}
doubleInt(5);

10
5
This method returns nothing.
This method is improperly written.

14. What will the value of yourBankAccount be after the method call?
int yourBankAccount = 0;
public void depositMoney(int bankAccount, int deposit)
{
bankAccount += deposit;
}
depositMoney(yourBankAccount, 1000000);

1000000
0
100000
The code will error.

15. Write a method that will ask for user input until user inputs the String “no”. Allow the user to input any capitalization of the String “no” (“no”, “No”, “NO”, “nO”) Also, have the method return the number of loops.
Use readLine to get the user input.

public int loopTillNo()
{
int count = 0;
while(!readLine(“Again?”).equals(“no”))
{
count++;
}
return count;
}
public int loopTillNo()
{
int count = 0;
while(readLine(“Again?”).toLowerCase().equals(“no”))
{
count++;
}
return count;
}
public int loopTillNo()
{
int count = 0;
while(readLine(“Again?”).equals(“no”))
{
count++;
}
return count;
}
public int loopTillNo()
{
int count = 0;
while(!readLine(“Again?”).toLowerCase().equals(“no”))
{
count++;
}
return count;
}

16. What will this method call print?
public void patternGrid(int rows, int columns, char symbol)
{
for(int m = 0; m < rows; m++)
{
for(int n = 0; n < columns; n++)
{
System.out.print(symbol);
}
System.out.println();
}
}
patternGrid(3,4,’#’);

####
####
####
###
###
###
####
####
####
####
This code will error.

17. What would this method call output?
public int myMethod(int num)
{
while(num / 10 >= 10)
{
num /= 10;
}
return num;
}
myMethod(830)

830
30
0
8
83

18. What is the return value of this method call?
public static String someWhereInTheMiddle(int number)
{
String stringNum = “” + number;
int middle = stringNum.length()/2;
if(stringNum.length() % 2 == 1)
{
return stringNum.substring(middle,middle+1);
}
else
{
return stringNum.substring(middle-1,middle+1);
}
}
someWhereInTheMiddle(188603)

8
6
7
86

19. What is returned by this method call: translator(“pokemon”)?
public String translator(String word)
{
return word.substring(1) + word.charAt(0) + “ay”;
}

“pokemonpay”
“okemonpay”
“okemonay”
This method call will error.

20. What will this method call output?
public int myMethod(boolean x, boolean y)
{
if(!x)
{
if(y)
{
return 1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
myMethod(false,false)

1
0
-1
This method call will error.

21. What could the method signature for a Karel command look like?

public void move()
{
//some code
}
public String move()
{
//some code
}
public int move()
{
//some code
}
public boolean move()
{
//some code
}

22. What will this method call print to the screen?
public static void someMethod(int a, String b, int c)
{
System.out.println(b + ” ” + a + “, ” + c);
}
someMethod(9,”Mar”, 1250)

9 Mar 1250
Mar 9 1250
9, Mar 1250
Mar 9, 1250

23. Write a method that loops forever until the user inputs the correct secret password or until the user fails to enter the correct password 10 times. The secret password the program should look for is the String “secret”
Use readLine(String prompt) for user input

public void secretPassword()
{
int count = 1;
while(true)
{
if(count == 10)
{
System.out.println(“You are locked out!”);
return;
}
if(readLine(“Password?”).equals(“secret”))
{
System.out.println(“Welcome!”);
return;
}
count++;
}
}
public void secretPassword()
{
int count = 0;
while(true)
{
if(count == 10)
{
System.out.println(“You are locked out!”);
}
if(readLine(“Password?”).equals(“secret”))
{
System.out.println(“Welcome!”);
}
count++;
}
}
public void secretPassword()
{
int count = 0;
while(true)
{
if(count == 10)
{
System.out.println(“You are locked out!”);
return;
}
if(readLine(“Password?”).equals(“secret”))
{
System.out.println(“Welcome!”);
return;
}
count++;
}
}
public void secretPassword()
{
int count = 0;
while(true)
{
if(count != 10)
{
System.out.println(“You are locked out!”);
return;
}
if(!readLine(“Password?”).equals(“secret”))
{
System.out.println(“Welcome!”);
return;
}
count++;
}
}

24. What will this method call output?
public String yesOrNo(boolean myBoolean)
{
if(myBoolean == true)
{
return “Yes”;
}
else
{
return “No”;
}
}
yesOrNo(true)

“No”
“Yes”
0
This program will error

25. What will this method call output?
public boolean someMethod(int number)
{
if(number % 2 == 0)
{
return true;
}
else
{
return false;
}
}
someMethod(9990)

false
0
“true”
true

26. What will this method call output?
public int mysteryMethod(String x, char y)
{
int z = 1;
for(int i = 0; i < x.length(); i++)
{
if(x.charAt(i) == y)
{
z++;
}
}
return z;
}
mysteryMethod(“Karel The Dog”, ‘e’);

1
2
3
4

27. What is wrong with this method definition?
public String outputString(String input)
{
System.out.println(input);
}

The method should not be public
Improper return value
Improper variable names
All of the above

28. What kind of error does this method cause?
public void buggyMethod(String x)
{
for(int i = 0; i <= x.length(); i++)
{
System.out.println(x.substring(i, i+1));
}
}
buggyMethod(“BUG”);

Compile Time Error: Unexpected return value
Runtime Error: String index out of range
Compile Time Error: Missing return value
Compile Time Error: Syntax Error in method definition

29. What will this method call print to the screen?
public void numberMadness(double myDouble)
{
int myInt = (int) myDouble;
String myString = “”;
while(myInt != 0)
{
myString = myInt % 10 + myString;
myInt /= 10;
}
System.out.println(myString);
}
numberMadness(12345)

12345
54321
123
543

30. Why do we use methods in Java?

To make code easier to understand
To avoid repeated code
To simplify code
All of the above

31. What does this method call output?
public int trickyMethod(int x, int y)
{
int sum = 0;
while (x <= 10)
{
sum += x % y;
x++;
y++;
}
return sum;
}
trickyMethod(3,1)

9
10
11
12

32. What is an object in Java?

An object is a template for how to make new programs in Java.
An object is something that contains both state and behavior.
An object is a single part of a computer program.
An object is a list of variables.

33. What is the difference between a class and an object?

Objects are instances of classes. The class is the general template, and the object is the specific version.
Classes are instances of objects. The object is the general template, and the class is the specific version.
A class has a run method or main method but an object does not.
An object is a way to refer to a Java program and a class refers to the programming environment.

34. What does it mean to be a client of a class?

Being a client of a class means that there a single method that we can use.
Being a client of a class means that we are the author of the class implementation.
Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.
Being a client of a class means that the class has documentation.

35. What is a constructor in Java?

A constructor is something that holds the private state of an instance.
A constructor allows us to create a new instance of a class, usually initializing instance variables.
A constructor is a method with instructions on how to use a class.
A constructor is a syntax rule in Java for placing curly brackets.

36. 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.

37. What is the difference between a getter method and an accessor method?

A getter method allows you to get the value of a field while a accessor method sets the value of the field.
A getter method allows you to get the value of a field while a accessor method is not often used in Java.
A getter method gets the name of the class while an accessor method gets the value of the field.
There is no difference. They refer to the same idea.

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

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

39. Suppose we have the following method in our class:
public int sum(int one, int two)
Which of the following are valid methods to also have in the class?  I:
double sum(double one, double two)
II:
int sum(int one, int two, int three)
III:
int sum(int x, int y)
IV:
double sum(int one, double two)

None, it is not possible to have two methods in the same class that have the same name.
I only
III only
I II and IV
IIIIII, and IV

40. 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

41. 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.

42. 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.

43. 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

44. 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

45. How do you call the superconstructor from a subclass?

You call it by making a call with the word super in the subclass constructor.
You call it by making a call with the word this or super in the subclass constructor.
You call it by making a call with the word superclass in the subclass constructor.
You call it by referencing the subclass from within the superclass constructor using instance variables.

46. Say you are trying to create a set of classes that represent tools. You will have a Tool class as well as a Hammer class and Screwdriver class. Hammer and Screwdriver are both subclasses of Tool. Which is the proper first line of each class?

public class Tool extends Screwdriver  public class Screwdriver extends Tool
public class Hammer extends Tool  public class Screwdriver extends Tool
public class Hammer extends Tool  public class Screwdriver extends Hammer
public class Hammer extends Tool  public class Screwdriver extends Super

47. What is the definition of polymorphism?

Polymorphism means that an instance of a class can have a different behavior depending on the constructor.
Polymorphism means that a subclass can override the instance variables in the super class.
Polymorphism lets a method do different things depending on the type of the object it is called on.
Polymorphism in Java means that different classes can have different methods.

48. What method must a class contain to implement the Comparable interface?

public int comparable(T other)
public boolean compare(T other)
public boolean equals(T other)
public int compareTo(T other)

49. 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

50. 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

51. 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;

52. 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

53. 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

54. 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

55. 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.

56. 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

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

58. 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”);

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

60. 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

61. 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()

62. 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

63. 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

64. 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

65. 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

66. 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

67. 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
 

"Is this question part of your assignment? We Can Help!"