You are on page 1of 23

CSCI 1110

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

8. What is the output of the following main method?


public static void main(String[] args){
int a, b;
a = 3; b = 4;
a = b;
System.out.println("a + " " + b);
a = a * 2;
b = a + 2;
System.out.println(a + " " + b);
a = a + b;
b = a - b;
System.out.println(a + " " + b);
}
a) 4 4
8 10
18 8
b) 3 4
6 8
12 -4
c) 4 4
8 10
18 -2
d) 4 4
8 6
14 8
9. What is output of the following class?
public class VariableExample{
public static void main (String [] args){
int a, b, c, d;
a=20; b=1; c=0;
d=b+c;
System.out.println (a + " " + b + " " + c + " " + d);
b=d+b;
d=d+1;
a=a/2;
c=a+b;
System.out.println (a + " " + b + " " + c + " " + d);
}
}
a) 20 1 0 1
10 2 12 2
b) 20 1 0 1
8 4 6 1
c) 10 1 0 0
1 2 5 7
d) 20 1 0 1
10 1 12 1

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

11. What is the output for the following code snippet?


int x=2, y=3;
System.out.println((x<=3));

a) true
b) false
c) it will give a compile error
d) nothing will print to the screen

12. What is the output of the following code snippet?


int x =4, y=2;
System.out.println(!(x > y));

a) 4
b) 2
c) false
d) true

13. What is the output of the following code snippet?


int x =4, y=2;
System.out.println( x + 2 > y || y + 2 > x);

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

15. A while loop is known as


a) a post test loop
b) a pre test loop
c) an infinite loop
d) a stopping loop

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

17. What is the output of the following snippet of code?


for (int i=0; i<10; i+=2){
System.out.print(i + " ");
}

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.

1: public class Questions {


2: public static void main (String [] args){
3: double cost = 16.25;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx //declare and assign
cost
4: double tax = 0.11;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx //declare and assign
tax
5: double total = cost +(cost*tax); xxxxxxxxxxxxxxxxxxxxxxxx //calculate
total
6: System.out.println("Total = $" + total);xxxxxxxxxxxxxxxxxx //print
7: }
8: }

24. Line 3 should be:


a) int cost = 16.25;
b) double cost = 16.25;
c) String cost = 16.25;
d) cost = 16.25;

25. Line 4 should be:


a) int tax = 0.11;
b) double tax = 0.11;
c) double tax = 11.0;
d) String tax = 11.0;

26. Line 5 should be:


a) total = cost + (cost*tax);
b) double total = cost + (cost*tax);
c) int total = cost*tax;
d) int total = cost + (cost*tax);

27. Line 6 should be:


a) System.out.println("Total = " + total);
b) System.out.println("Total = $" + cost);
c) System.out.println("Total = $" + total);
d) System.out.println("Total = $" + (total + tax));

7
Q28 to Q31 - Use the provided code to trace the output.

1. public class TestMathClass{


2. public static void main(String [] args) {
3. double x1, x2, result;
4. x1 = 2.0;
5. x2 = 3.0;
6. result = Math.min(x2, x1);
7. System.out.println("result is " + result); //output
8. result = Math.max(x1, x2);
9. System.out.println("result is " + result); //output
10. result = Math.pow(x2, x1);
11. System.out.println("result is " + result); //output
12. result = Math.sqrt(Math.pow(x1,x2)+1);
13. System.out.println("result is " + result); //output
18. }
19. }

28. Line 7 will output:


a) result is 2
b) result is 3.0
c) result is 2.0
d) result is 3

29. Line 9 will output


a) result is 3.0
b) result is 3
c) result is 2.0
d) result is 2

30. Line 11 will output


a) result is 9
b) result is 9.0
c) result is 8.0
d) result is 8

31. Line 13 will output


a) result is 4.0
b) result is 4
c) result is 3.0
d) result is 3

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

32. Line 7 should be:


a) int num = kb.nextInt();
b) num = kb.nextDouble();
c) num = kb.nextInt();
d) int num = kb.nextDouble();

33. Line 8 should be


a) if (num % 2 != 0)
b) if (num / 2 == 0)
c) if (num % 2 == 0)
d) if (num / 2 != 0)

34. Line 10 should be


a) if
b) else
c) if (num!=0)
d) if else

35. What is the output of the following?


String alpha = "abcd";
System.out.println( alpha.charAt(1));

a) It will print a
b) It will print 'a'
9
c) It will print b
d) It will print 'b'

36. What does the following code snippet output?

public class StringExamples{


public static void main (String [] args){
String city1 = "Dartmouth";
String city2 = "Halifax";
System.out.println(city1.length() + " " + city2.length());
}
}

a) 97
b) 79
c) 86
d) 68

37. What does the following code snippet output?

public class StringExamples2{


public static void main (String [] args){
String city1 = "Dartmouth";
String city2 = "Truro";
for (int i=city2.length();i>=0; i--)
System.out.print(city1.charAt(i) + " ");
}
}

a) Dartmo
b) Dartm
c) omtraD
d) mtraD

38. What does the following code snippet output?

public class StringExamples3{


public static void main (String [] args){
String city1 = "Dartmouth";
String city2 = "Truro";

for (int i=0;i<city2.length(); i+=3)


System.out.println(city1.substring(i, city2.length()));
}
}

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

Use the following array for Q40-Q42:


An Array of integers called arr

10 6 2 0 5

40. What is the value stored at index 3?


a) 0
b) 2
c) Nothing
d) 6

41. What would following output?


System.out.println(arr[1]+arr[4]);

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;

43. What does the following output?


for (int i=2; i>=0; i--){
for (int j=1;j <=3; j++)
System.out.print((j * i) + " ");
System.out.println();
}

a) Nothing will print
b) This will cause a compile error
c) 2 4 6
123
000
d) 2 2 2
111
000

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

3. What is printed by the following Java segment?



for (int i = 1; i < 5; i++) {
for(int j = 1; j < 5; j++){
if (i == j)
System.out.print("O");
else
System.out.print("X");
}
System.out.println();
}
a. OXXX
XOXX
XXOX
XXXO
b. XOOO
OXOO
00XO
000X
c. OXXO
OXXO
OXXO
OXXX
d. OOOX
OOX0
OXOO
XOOO

4. A class in java is like


a. a variable
b. an object
c. an instantiation of an object
d. a blueprint for creating objects

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)

7. What is the syntax error in the following method definition?


public static String parameter(double r){
double result;
result = 2 * 3.14 * r;
return result;
}
a. The method does not return the value result.
b. The value that is returned does not match the specified return type.
c. The variable result is set but never used.
d. The method does not specify the result return type.

8. What is the output of the following Java program?


public class Output{
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.print(myFun(i) + " ");
}
}
public static int myFun(int perfect) {
return ((perfect - 1) * (perfect - 1));
}
}
a. 1 4 9 16
b. -1 0 1 4 9
c. -1 0 1 4
d. 1 0 1 4

9. When an array is passed as a parameter into a method:


a. A reference (“address”) to the original array is passed
b. A copy of the complete array is passed
c. A copy of the first element is passed
d. Only the length of the original array is passed

10. Consider the following code snippet:


int[][] numarray = {{ 3, 2, 3 },{ 0, 0, 0 }};
System.out.print(numarray[0][0]);
System.out.print(numarray[1][0]);
What is the output of the given code snippet?
a. 31
b. 30
c. 00
d. 03

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.

17. The signature of a method consists of the following


a. Return type, method name and types of input parameters
b. Method name and types of input parameters
c. Method name, types of input parameters and their order
d. Return type, method name, types of input parameters and their order

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

19. The advantage of a deep copy in a constructor is the following:


a. deep copy saves memory
b. deep copy provides better encapsulation
c. deep copy enables the constructor to be used in multiple classes
d. none of the above – you cannot create a deep copy

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.

21. Aggregation can be identified by


a. An “is-a” relationship between objects
b. A “has-a” relationship between objects
c. No relationship between objects
d. A “may be a” relationship between objects

22. Consider the following code snippet:


public class Book {
private String title;
private int pages;
public Book (String bookTitle, int pageNum)
{ . . . }
}

Assuming the following declaration, which statement creates an object of type Book?
Book novel;

a. novel = new Book("The Great Gatsby", "200");


b. novel = new Book("The Great Gatsby ", 200);
c. novel = new Novel ("The Great Gatsby ", 200);
d. novel = Book (200, " The Great Gatsby ");

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?

Select the correct answer here:


a. Demo program 1 only
b. Demo program 2 only
c. Demo program 3 only
d. Demo programs 1 and 2
e. Demo programs 1 and 3

//Demo program 1 //Demo program 2 //Demo program 3


public class DogDemo { public class DogDemo { public class DogDemo {
public static void main (String [] args){ public static void main (String [] args){ public static void main (String [] args){
Dog husky = new Dog (2, "Zara"); Dog husky = new Dog ("Zara", 2); Dog husky = new Dog ("Zara", 2);
Dog lab = new Dog (1, "Happy"); Dog lab = new Dog ("Happy", 1); Dog lab = new Dog ("Happy", 1);
System.out.println(husky); System.out.println(husky); System.out.println(husky);
System.out.println(lab); System.out.println(lab); System.out.println(lab);
System.out.println(); System.out.println(); System.out.println();
if (husky.isOlder(lab)) if (husky.isOlder(lab)) if (husky.isOlder(lab))
System.out.println(husky.getName() + " is System.out.println(husky.getName() + " is older System.out.println(husky.getName() + " is older
older than " + lab.getName()); than " + lab.getName()); than " + lab.getName());
else
else else
System.out.println(lab.getName()+ " is older
System.out.println(lab.getName()+ " is older System.out.println(lab.getName()+ " is older
than " + husky.getName());
than " + husky.getName()); than " + husky.getName());
System.out.println(husky.speak("Woof"));
System.out.println(husky.speak()); System.out.println(husky.speak("Woof"));
}
} System.out.println(husky.speak()); }
} }
}

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

3. This keyword indicates that a class inherits from another class


a. derived
b. super
c. extends
d. this

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

5. What is the output of the following code segment?


ArrayList<String> cities = new ArrayList<String>();

cities.add("Atlanta");

cities.add("Boston");
for (int i = 1; i < cities.size(); i++)

cities.add(i, "+");
System.out.println(cities);

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

9. What does the following output?


public class Demo_P{
private int a;
private static int b = 10;
public Demo_P(int value) {
a = value;
}
public void Method1(){
b-=2;
a++;
}
public int Method2(){
return a-b;
}
public static void main(String[] args) {
for (int j=0;j<3;j++){
Demo_P obj1 = new Demo_P(10);
obj1.Method1();
System.out.print(obj1.Method2()+ " ");
}
}
}
19
a. 333
b. 357
c. 345
d. 157

10. What is the output of the following code?


import java.util.ArrayList;
public class Q4C {
public static void main (String args[]){
ArrayList <String> aList = new ArrayList<String>();
String temp;
aList.add("AB");
aList.add("CD");
aList.add("EF");
aList.add(0,"UV");
aList.add(0,"WX");
aList.add(0,"YZ");
System.out.println(aList);
}
}
a. [AB, CD, EF, UV, WX, YZ,]
b. [YZ, WX, UV, AB, CD, EF]
c. [UV, WX, AB, CD, EF, YZ]
d. [UV, WX, YZ, AB, CD, EF]

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.

2. Consider the following class:


public class Book{
private int pages;
public Book(){
}
public void setPages(int n){
pages = n;
}
public int getPages(){
return pages;
}
}

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
}

public class DictionaryDemo{


public static void main(String[] args)
{
Dictionary webster = new Dictionary(“English”, “English”, 1000, 52500);
System.out.println(webster);
}
}

The output is:


English to English Dictionary
Number of pages: 1000
Number of definitions: 52500

3. Consider the following class definition:

public class Shoe{


private double size;
public Shoe(double s){ size=s; }
public double getSize(){return size;}
public void setSize (double s) {size=s;}
public String toString(){
return ("Shoe Size: " + size);
}
}

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.

public class RunningShoe extends Shoe {


private String colour;
public RunningShoe (String colour, double size){
super(size);
this.colour=colour;
}
public void setColour (String c) {
colour=c;
}
public String getColour() {
return colour;
}
public String toString (){
return "Size: " + getSize() + " Colour: " + colour;
}
}

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

public int indexOfSmallest(ArrayList<Integer> a){


int smallest = a.get(0);
int index = 0;
for(int i = 1; i<a.size(); i++){
if (a.get(i)< smallest){
smallest = a.get(i);
index = i;
}
}
return index;
}

23

You might also like