Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
37 views13 pages

Cmpt270 Midterm 2018 Answers PDF

Midterm 270 compsci

Uploaded by

xnyygrskkb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views13 pages

Cmpt270 Midterm 2018 Answers PDF

Midterm 270 compsci

Uploaded by

xnyygrskkb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: NSID: Student #:

University of Saskatchewan
Department of Computer Science
CMPT 270, 2018/19 Term 1
Instructor: Shane Giroux
Midterm Examination
October 25, 2018
Marks: 55 Time: 80 minutes

Instructions:

• Write your name, NSID, and student number at the top of every page.

• Please fully fill out the opscan form and record all multiple choice answers on that form with a
pencil.

• Hand in every page, even if you rip it off the booklet.


• Read every question carefully.

• Write the answers in the space provided. Use the back of the page if you need extra space (or for
rough work).

• The mark value of each question is provided in the left margin.


• No textbook. No calculators. No notes. No laptops. No phones.

• If you have a clarifying question, please raise your hand and I or the TA will come to you. I will
only answer clarification questions. No hints.

Academic Honesty
This exam is an individual undertaking – cheating on an exam is considered a serious offence by the
University and can be met with disciplinary action, including suspension or expulsion. By handing in
this exam you affirm that this work is wholly your own.

Please do not write in the space below on this page. For marking purposes only.

Page: 1 2 3 4 5 6 7 8 9 Total

Marks: 4 4 5 5 4 3 10 10 10 55

Score:
Name: NSID: Student #:
(1) 1. Which of the following can be executed using the Java Virtual Machines:
A. machine code
B. byte code
C. source code
D. all of the above
E. none of the above

(1) 2. The static keyword in front of a variable declaration in a class has the following meaning:
A. the variable cannot be changed and is effectively a constant
B. the variable is shared between all instances of the class
C. the variable can be accessed by any classes within the same package

D. all of the above

E. none of the above

(1) 3. To call a static method doThis() in the A class (which has a constructor with no parameters), which of
the following is preferred:
A. A myVar = new A();
myVar.doThis();
B. A myVar;
myVar.doThis();
C. A.doThis();
D. all of the above
E. none of the above

(1) 4. A list of players on a sports team can be categorized as the following type of object:
A. entity
B. container
C. interface
D. control
E. all of the above
F. none of the above

CMPT 270 Midterm Page 1 of 11 Oct. 25, 2018


Name: NSID: Student #:
For the following nine questions (5–13), consider the code which can be found on the last page.

(1) 5. What occurs during the second line when running the following from a main method:

P myVar = new P();


myVar.doSomethingP();

A. the z instance variable myVar object changes to 5


B. the data of the object does not change
C. compiler error
D. runtime error
E. E. it depends

(1) 6. What occurs when running the following from a main method:

Q myVar = new Q();

A. w instance variable is set to 4


B. x instance variable is set to 1
C. y instance variable is set to 2
D. z instance variable is set to 3
E. all of the above
F. none of the above

(1) 7. What happens when the following code is executed in a main method?

P myVar = new P();


myVar.doSomethingQ();

A. it works (if doSomethingQ() works)


B. compiler error
C. runtime error
D. it depends

(1) 8. What happens when the following code is executed in a main method?

Q myVar = new Q();


myVar.doSomethingP();

A. it works
B. compiler error
C. runtime error
D. it depends

CMPT 270 Midterm Page 2 of 11 Oct. 25, 2018


Name: NSID: Student #:

(1) 9. If the contents of doSomethingQ() is the following segment:


P myVar = new P();
P myVar2 = new P();
if (myVar == myVar2) System.out.println("equal!");
else System.out.println("not equal!");

What does this output when executed?


A. “equal!”
B. “not equal!”
C. compiler error
D. none of the above
(1) 10. If the contents of doSomethingQ() is the following segment:
System.out.println(x);
A. it works
B. compiler error
C. runtime error
D. none of the above
(1) 11. If the contents of doSomethingQ() is the following segment:
System.out.println(y);
A. it works
B. compiler error
C. runtime error
D. none of the above
(1) 12. If the contents of doSomethingQ() is the following segment:
System.out.println(z);
A. it works
B. compiler error
C. runtime error
D. none of the above
(1) 13. What happens when the following code is executed?
P myVar = new Q();
A. A P object is created
B. A Q object is created
C. compiler error
D. none of the above

CMPT 270 Midterm Page 3 of 11 Oct. 25, 2018


Name: NSID: Student #:

(1) 14. If an interface called Something contains a method String doSomething(String s), then:
A. Every class that implements the interface must override a method implementation from
the interface
B. Classes that implement the interface can optionally override a method implementation
from the interface
C. Every class that implements the interface must have a method called doSomething that
takes in a String, and returns a String
D. No class that implements the interface can have such a method

(1) 15. With an interface called Something, it is possible to declare LinkedList<Something> list;
A. true
B. false
(1) 16. A test driver:
A. checks all the test cases
B. does exhaustive testing on programs
C. executes all stubs
D. involves testing by humans

(1) 17. Regression testing involves:


A. minimal output to see which test cases fail
B. only prints out output of methods
C. usually depends on user input
D. all of the above
E. none of the above
(1) 18. Top-down testing involves:
A. if class A uses class B, writing class A first
B. if class A uses class B, writing class B first
C. writing sub-classes before super-classes
D. writing pseudocode
E. none of the above

CMPT 270 Midterm Page 4 of 11 Oct. 25, 2018


Name: NSID: Student #:

(1) 19. The class


public class A{
public static void main(String[] args) {
int myNumber = 2;
int[] myArray = {2};
addTwo(myNumber, myArray);
System.out.println(myNumber + ", " + myArray[0]);
}
public static void addTwo(int number, int[] array) {
number = number + 2;
array[0] = array[0] + 2;
}
}
will print the following to the console:
A. 2, 2
B. 2, 4
C. 4, 2
D. 4, 4
E. compile error
F. runtime error

(1) 20. Sets in Java:


A. store elements without duplicates
B. do not guarantee that order be preserved
C. uses a Java interface
D. allows multiple possible data structures
E. all of the above

(1) 21. In order to use the Scanner class, it is necessary to use import java.util.Scanner; at the
top of the file
A. true
B. false

(1) 22. In order to use the String class, it is necessary to use import java.lang.String; at the top of the file
A. true
B. false

CMPT 270 Midterm Page 5 of 11 Oct. 25, 2018


Name: NSID: Student #:

(1) 23. The following code would be good to:


Iterator<String> iter = m.iterator();
while (iter.hasNext()){
String name = iter.next();
}

A. iterate through a map m one element at a time


B. iterate through a String m one letter at a time
C. iterate through a set m one element at a time
D. iterate through a map m in the order elements were added E. all of the above

(1) 24. In order to use the TreeSet class to store data from class A, then
A. A must implement a binary search tree
B. A must implement the Comparable interface
C. A must subclass the TreeSet class
D. A must subclass the Set class
E. A must subclass the Collection class

(1) 25. High coupling between classes is:


A. a sign of high dependency between classes
B. should be avoided
C. makes it difficult to use a class
D. makes it difficult to test
E. all of the above

CMPT 270 Midterm Page 6 of 11 Oct. 25, 2018


Name: NSID: Student #:

(10) 26. (a) Draw a UML class diagram (with data and operations) to represent the following specification.
You do not need to include any information not mentioned in the specifications.

Design a system to store information on a collection of books. Each book can either be a physical
book, or an eBook. In either case, they have a title, authors (just a single string), ISBN number,
and price. Physical books have a number of pages, whereas eBooks have a size in megabytes.
There should be a way to create these books, there should be ways to access each property, a
way to change the price, and there should be a way to return a string representation of all
information on each book in the order in which they were added to the system. There should be
a way to get a string representation of all books (that includes all information on each book) in
the collection, and to search the collection with an ISBN number.

CMPT 270 Midterm Page 7 of 11 Oct. 25, 2018


Name: NSID: Student #:

(3) (b) Does it make sense to make any classes created above abstract classes? Which ones? Why?

Yes, it makes sense for Book class to be abstract, because every Book can either be a
physical book, or an eBook. There cannot be a book that is neither. So, it makes sense for there
to be a Book class to capture the information and operations common to both types, but we do
not want to allow the creations of Books.

(7) (c) Implement the method that returns the string representation of all books in the collection (that
includes all the information about each book) in the order they were added. Other methods in other
classes can do some of the work of retrieving information about a single book. (If you are using a
method from the Java API but cannot remember the name of it, just use a descriptive name that
implies what it does.) Explain why it works.
public String toString() {
String result = "";
for (int i=0; i<books.size(); i++) {
result = result + books.get(i).toString() + "\n";
}
}
This works because if an object is a PhyBook, it will use the toString() method in that class (thus
including information on the number of pages), and if it’s an eBook, it will use the toString() method
in its class.

CMPT 270 Midterm Page 8 of 11 Oct. 25, 2018


Name: NSID: Student #:

(7) 27. (a) Write a Student class with two fields for “Name” and “ID number”, both strings. It only has a
constructor that has preconditions that neither the name nor student number should be null,
and also student number has to be 6 characters. If the preconditions are not satisfied, it should
throw a runtime exception. Nothing else is required in this class besides the constructor and
data. (No comments needed.)

public class Student {


private String name;
private String id;
public Student(String name, String id) {
if (name == null || id == null || id.length() != 6) {
throw RuntimeException;
}
this.name = theName;
this.id = id;
}

(3) (b) How would you adjust the constructor above (just write the part that is different) so that there is
another precondition that the ID number must be all numerical digits. Recall that the method
Integer.parseInt(String s) throws a NumberFormatException exception if s does not represent an
integer. Your method should throw this same type of exception if a noninteger is passed in.
Add in Integer.parseInt(theIdentifier) as second line of code.

If parseInt throws a NumberFormatException, this will continue to pass it to


the calling method

CMPT 270 Midterm Page 9 of 11 Oct. 25, 2018


Name: NSID: Student #:

(Blank page)

CMPT 270 Midterm Page 10 of 11 Oct. 25, 2018


Name: NSID: Student #:

This code is relevant to questions 5–13.

package pack1; //in a file called P.java


public class P {
int x;
protected int y;
private int z;

public P() {
x = 1;
y = 2;
z = 3;
}

public void doSomethingP() {


z = 3;
}
}

package pack2; //in a file called Q.java


import pack1.P;
public class Q extends P {
private int w;

public Q() {
super();
w = 4;
}

public void doSomethingQ() {



}

CMPT 270 Midterm Page 11 of 11 Oct. 25, 2018

You might also like