Professional Documents
Culture Documents
PRACTICE QUESTIONS
TEST 1 QUESTIONS (multiple choice questions copied from TEST 1 review questions)
1. A compiler finds:
a) Logical errors
b) Logical and syntax errors
c) Syntax errors
d) None of the above
2. Java keywords
a) begin with upper case letters
b) begin with lower case letters
c) don't exist
d) can be used to name variables
3. What will be the result of the following statement double x = 5;
a) x will store the value 5.0
b) You will get a compile error because the value isn't a double
c) x will store the value 5
d) You will get a compile error because x is a keyword
4. What does the following program print?
public class Q1b {
public static void main (String [] args){
System.out.print("a");
System.out.println("\nb");
}
}
a) a
b
b) a b
c) a
\nb
d) ab
5. What is the output of the following print statement?
System.out.println("10" + 2* 3);
a) 16
b) 1023
c) 106
d) 36
6. What is the output of the following print statement?
System.out.println( 2 * 1 / 4 + 2 * 2);
a) 4.5
b) 4
c) 6
d) none of the above
1
7. What is the output of the following code snippet?
int x =2, y=3;
System.out.println((x+2) + (y/3.0) * 2);
a) 10
b) 10.0
c) 6
d) 6.0
2
10. If one side of logical operator AND (&&) is false
a) it will evaluate to true
b) it will evaluate to false
c) it will give a compile error
d) none of the above
a) true
b) false
c) it will give a compile error
d) nothing will print to the screen
a) 4
b) 2
c) false
d) true
a) 4
b) 6
c) true
d) false
3
Use the program below to answer questions 13 and 14:
import java.util.Scanner;
public class Trace_stopLight_P {
public static void main(String[] args) {
int lightRed, lightGreen;
Scanner keyboard = new Scanner(System.in);
System.out.print("Input a 0 or 1: ");
lightRed = keyboard.nextInt();
System.out.print("Input a 0 or 1: ");
lightGreen = keyboard.nextInt();
String action1 = "nothing";
if (lightGreen != 1)
action1 = "Dance";
else
action1 = "Spin";
System.out.print(action1 + " ");
if (lightRed==1 || lightGreen==0)
action1="Sing";
if (lightGreen==1 && lightRed == 0)
action1="Whisper";
else
action1="Yell";
System.out.print(action1);
}
}
13. If you had the following input, what would the output be?
Input Output
01 ?
a) Spin Whisper
b) Dance Yell
c) Spin Sing
d) Dance Whisper
14. If you had the following input, what would the output be?
Input Output
10 ?
a) Spin Whisper
b) Dance Yell
c) Spin Sing
d) Dance Whisper
4
16. What is the output of the following snippet of code?
int i=0;
while (i<10) {
if (i%2!=0)
System.out.print (i + " ");
i++;
}
a) 02468
b) 0 2 4 6 8 10
c) 13579
d) 3579
a) 02468
b) 2 4 6 8 10
c) 0 2 4 6 8 10
d) This will not compile
18. How many stars will this for loop display?
int star;
for(star = 9; star >0; star++)
System.out.print ("*");
a) 9
b) 0
c) 10
d) Infinite (it will keep printing until the program is terminated)
19. What would the output be of the following program?
public class Q21 {
public static void main (String [] args){
System.out.print("a");
System.out.println("\\b");
}
}
a) a
\b
b) a\\b
c) a
\\b
d) a\b
5
20. What would be the output from the following program?
public class Q22{
public static void main (String [] args){
System.out.println(1+ 1 + "2" + "3");
System.out.println(1 + "2" + 2);
}
}
a) 1123
122
b) 223
122
c) 7
5
d) 223
13
21. What does the following statement print?
System.out.println(1 * 5/2 + "3");
a) 2.53
b) 23
c) 1523
d) It won’t print because there is an error.
22. Which of the below identifiers are correct?
A. an.swer
B. an_swer
C. public
D. $error
E. 5_error
F. $101
G. error_5
a) A, B, D, E
b) B, D, F, G
c) B, C, D, E
d) C, D, E, F
23. What is printed by the following program?
public class Q25{
public static void main(String args[]) {
int x = 2;
int y = 3;
int z = x * y;
System.out.println("z = " + z);
x = 2; y = 2;
System.out.println("z = " + z);
}
}
a) z = 5
z=5
b) z = 6
z=6
c) z = 6
z=4
d) z = 5
z=4
6
Q24-28 use the following code
The following code for the below program is incomplete (lines of code have been blacked out).
This program assigns the value 16.25 representing a cost in dollars to a variable called cost. The
program also assigns tax amount of 11% to variable called tax. The program determines the
final cost after applying the tax and assigns it to a variable called total. Finally, the program
prints the total cost as follows: Total = $. . . Do not concern yourself with controlling the display
of the decimal point. Answer the multiple-choice questions below to complete the code.
7
Q28 to Q31 - Use the provided code to trace the output.
8
Q32-Q34 use the below code:
Complete the program below by filling in the blacked out lines of code. The program works as
follows, the program asks the user to enter an integer. If the user inputs an even integer, the
program prints "You entered an even number"; or the program tells the user – “You entered an odd
number”.
Sample output:
Please type an integer: 18
You entered an even number
Please type an integer: 63
You entered an odd number
1. import java.util.Scanner;
2. public class Question {
3. public static void main (String [] args){
4. int num;
5. Scanner kb = new Scanner (System.in);//declare Scanner kb
6. System.out.print("Enter an integer: ");
7. xxxxxxxxxxxxxxxxx //use Scanner to read in int
8. xxxxxxxxxxxxxxxxx //check to see if number is even
9. System.out.println("You entered an even number");
10. xxxxxxxxxxxxxxxxx //otherwise print it is an odd number
11. System.out.println("You entered an odd number");
12. }
13. }
a) It will print a
b) It will print 'a'
9
c) It will print b
d) It will print 'b'
a) 97
b) 79
c) 86
d) 68
a) Dartmo
b) Dartm
c) omtraD
d) mtraD
a) D a r t
t
b) Dartm
tm
c) tm
m
d) Dartmouth
tmouth
uth
10
39. The proper way to declare an integer array nums with 3 numbers of 5, 6 and 10 is
a. int [3] nums = (5,6,10);
b. int [] nums = {5,6,10};
c. int nums [] = (5,6,10);
d. int nums = {5,6,10};
10 6 2 0 5
a) 6
b) 10
c) 11
d) 8
42. What would be the proper way to update the first element of the array to 4?
a) arr (0) = 4;
b) [0] arr = 4;
c) arr [0] = 4;
d) arr [1] = 4;
11
TEST 2 QUESTIONS (multiple choice questions copied from TEST 2 review questions)
1. What is printed by the following code:
public static void main(String [] args) {
int a = 2;
int b = 3;
int c = 3;
a = mystery(a, b, c);
b = mystery(b, c, a);
c = mystery(c, a, b);
System.out.print(a + " " + b + " " + c);
}
public static int mystery(int a, int b, int c) {
a = a + c;
return a + b - c;
}
a. 5 6 8
b. 5 5 8
c. 5 6 4
d. 2 3 3
2. The proper way to declare an integer array nums with 3 numbers of 5, 6 and 10 is
a. int [3] nums = (5,6,10);
b. int [] nums = {5,6,10};
c. int nums [] = (5,6,10);
d. int nums = {5,6,10};
12
5. If the following statements were given in the main method:
double [] arr = {10.4, 2.1, 3.7};
double sum = add (arr);
What would the method header be of method add?
a. public static double add (double x )
b. public static double add (double [] x)
c. public static void add (double x)
d. public static void add (double [] x)
6. Which of the following is the correct header for a method definition named calcSum that accepts
four int arguments and returns a double?
a. public static double calcSum(int a, int b, int c, int d)
b. public static double calcSum()
c. public static calcSum(int a, int b, int c, int d)
d. public static int calcSum(double a, double b, double c, double d)
13
11. Which one of the following statements is correct for displaying the value in the second row and the
third column of a two-dimensional, size 3 by 4 array?
a. System.out.println(arr[2][1]);
b. System.out.println(arr[2][3]);
c. System.out.println(arr[3][2]);
d. System.out.println(arr[1][2]);
12. If a class is named Student, what name can you use for a constructor for this class?
a. Student
b. Student.java
c. void Student
d. any name can be used since the constructor can be overloaded
13. When defining a constructor, what do you specify for the type of value returned?
a. Primitive data type such as int
b. Class name such as Student
c. void
d. None – a constructor does not have an explicit return type
14. You are creating a class named Employee. Which statement correctly declares a constructor for this
class?
a. public void Employee()
b. void Employee()
c. public Employee()
d. public new Employee()
15. Which of the following declares a sideLength instance variable for a Square class that stores an
integer value?
a. public int sideLength;
b. private int sideLength;
c. private integer sideLength;
d. public integer sideLength;
16. The get and set methods in a class are also known, respectively, as
a. accessor and mutator methods
b. mutator and accessor methods
c. accessor and constructor methods
d. mutator and constructor methods.
18. When you implement a method with the same name as another method inside a class, you __________
the original method.
a. overload
b. override
c. copy
d. aggregate
14
20. Analyze the following code:
public class SomeClass {
private double info;
public SomeClass(double info) {
info = info;
}
}
a. The program has a compilation error because you cannot assign info to info.
b. The program does not compile because SomeClass does not have a default constructor.
c. The program will compile, but you cannot create an object of SomeClass with a specified
info. The object will always have info set to 0.
d. The program has a compilation error because it does not have a main method.
Assuming the following declaration, which statement creates an object of type Book?
Book novel;
For next six questions, please refer to the Dog class given below. Some statements are missing and by
answering the multiple-choice questions, you will build the missing pieces. Here’s a general description of the
Dog class.
Dog has two attributes: name (String) and age (int). The constructor sets both attributes. There are get and set
methods. In addition, the class has the following:
• A method called speak that returns a String “bark”
• Another method called speak that just returns a given String
• A method isOlder that returns true if “this” Dog object is older than another Dog object
• A method isSame that compares this Dog object with another Dog object and returns true if the name and
age are the same
• A method copy that returns a copy of this Dog object with the same name and age.
Code:
1. public class Dog { 21. //isOlder method
2. //attributes 22. public boolean isOlder (Dog d){
3. private String name; 23. //this line is still missing
4. private int age; 24. }
5. //Constructor
6.//this line is still missing 25. //isSame method
7. { 26. public boolean isSame (Dog d) {
this.name=name; 27. //this line is still missing
this.age=age; 28. }
8. }
9. //get and set methods 29. //copy method
10. public String getName() {return name;} 30. //missing code
15
11. public int getAge() {return age;} 31. //missing code
12. public void setName(String n){name=n;}
13. public void setAge (int a) {age=a;} 32. public String toString(){
14. //speak method 33. returnname + " is " + age + " years old";}
15. public String speak (){
16. return "bark";
17. }
18. //the second speak method
19. //this line is still missing
20. return s; }
23. Line 6 (choose the correct code to fill in the missing piece in line no. 6)
a. public Dog(String n, String a)
b. public void Dog(String name, int age)
c. public void Dog(String n, int a)
d. public Dog(String name, int age)
24. Line 19
a. public void speak(String bark){
b. public String speak(String bark){
c. public void speak(String s){
d. public String speak(String s){
25. Line 23
a. return this.dog>d.getDog();
b. return this.age>d.getAge();
c. return age>other.age();
d. return this.getAge()>age.other;
26. Line 27
a. return (name.equals(d.getName())&&age==d.getAge());
b. return this==d;
c. return this.name==d.name&&this.age==d.age;
d. return this.name.equals(d.getName())&&this.age==age;
27. Lines 30 and 31
a. public void copy(){
return new Dog(name, age);}
b. public Dog copy(){
return new Dog(name, age);}
c. public Dog copy(Dog d){
return new Dog();}
d. public void copy(Dog d){
return new Dog();}
16
28. Look at the three demo programs given below. What demo program will produce the following output?
NOTE: There were also programming questions from Test2 questions that you can do for practice
(Questions and answers for Test 2 are attached on Brightspace).
17
LAST TOPICS QUESTIONS – NEW QUESTIONS
1. This type of method cannot access any non-static member variables in its own class.
a. instance
b. void
c. static
d. non-static
2. A static method
a. Can be called by only one object
b. Can be called without object instantiation
c. Has to be called from inside the class
d. Cannot be called by non-static method
4. When you implement a method with the same name as another method inside a class, you __________ the
original method.
a. overload
b. override
c. copy
d. aggregate
a. [Atlanta, Boston]
b. [Atlanta, +, Boston]
c. [Atlanta, +, Boston, +]
d. No output because the program goes into an infinite loop.
6. The following is an explicit call to the superclass’s default constructor.
a. default();
b. class();
c. super(this);
d. super();
7. This is the name of a reference variable that is always available to an instance method and refers to the
object that is calling the method.
a. callingObject
b. calledObject
c. this
d. me
18
8. Look at the following classes:
public class Ground{
public Ground(){
System.out.println(“You are on the ground”);
}
public Ground(String groundColor) {
System.out.println(“The ground is “ + groundColor);
}
}
public class Sky extends Ground{
public Sky(){
System.out.println(“You are in the sky”);
}
public Sky(String skyColor) {
super(“green”);
System.out.println(“The sky is “ + skyColor);
}
}
What will the following program display?
public class Check{
public static void main(String[] args){
Sky object1 = new Sky(“blue”);
Sky object2 = new Sky();
}
}
a. The ground is green
The sky is blue
You are on the ground
You are in the sky
b. The sky is blue
The ground is green
You are in the sky
You are on the ground
c. The sky is blue
The ground is green
d. The ground is green
The sky is blue
11. Consider a method to reverse an ArrayList of BattleShip objects. If the original ArrayList is to be
reversed, what is the most appropriate header for the method to be used?
a. public static void reverse(ArrayList<Battleship> list)
b. public static ArrayList<String> reverse(ArrayList<String> list)
c. public static ArrayList<Battleship> reverse ()
d. public static void reverse(ArrayList<String> list)
12. To remove a node that is not the first node of a linked list conveniently, you need to have a reference to
a. the node that is before it
b. the node that comes after it
c. the node itself
d. null
13. What conditional confirms if a singly linked list is empty
a. if (head = = tail)
b. if (head = = 0)
c. if (head = = null)
d. if (head = = "Empty")
14. The code to add a new node to the front of a singly linked list, what node do you need access to:
a. the node that comes directly after the head node
b. the node that comes before the head node
c. the head node itself
d. the data of the head node
15. Which of the following statements about removing a node from a linked list is correct?
a. A node's data is discarded when it is removed from the linked list, and its memory space is immediately
reclaimed.
b. A node's data is returned to the program when the node is removed from the linked list, and its
memory space is immediately reclaimed.
c. A node's data is discarded when it is removed from the linked list, and its memory space is reclaimed
later by the garbage collector.
d. A node's data is returned to the program when the node is removed from the linked list, and its
memory space is reclaimed later by the garbage collector.
20
Extra Questions
These are programming questions that can help you study. They are not in Mimir, but you can use another IDE
to write the programs:
1. Write a model of a payroll system that deals with employees earning an hourly rate.
(a) Write an Employee class with the following specifications:
• name, ID, hourlyRate, hours and pay as the attributes
• constructor that sets the name, ID, hourlyRate and hours
• method calcPay that calculates the pay of the employee
(pay is straight hourly rate for each hour <=40 and 1.5 times the hourlyRate for each hour >40)
• toString method that returns the String with the name, ID and the pay.
(b) Write an EmployeeUser class that prompts the user to enter employee information, one on each line,
reads the information until a “*” is entered, and prints the name, ID and the pay for each employee.
(c) Write a StudentEmployee class that extends the Employee class. In addition to the attributes and
methods in the Employee class, this class maintains the list of courses and the marks, and a set method
for each course and its corresponding mark.
Create a class Dictionary that extends the Book class. The main test method for Dictionary and the
corresponding output are given. Fill in the rest of the code.
public class Dictionary extends Book
{
private int definitions;
private String from;
private String to;
//fill in code here
public Dictionary(String from, String to, int pages, int definitions){
this.from = from;
this.to = to;
setPages(pages);
this.definitions(definitions);
}
public String toString(){
String result = from + “ to ” + to + “ Dictionary\n”;
result += (“Number of pages: ” + getPages() + “\n”);
result += (“Number of definitions: ” + definitions + “\n”);
return result;
}
}
21
}
Write a class called RunningShoe that extends the Shoe class. The RunningShoe class has colour as its instance
variable, a constructor that sets the size and the colour, appropriate get and set methods, and a toString method
that prints the size and the colour of the RunningShoe.
22
4. Write a method in a demo program that returns the index of the smallest integer in an arraylist of
integers. (assumes that a has at least one element and if the smallest integer is repeated, it returns the
index of the first occurrence).
23