• No results found

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

N/A
N/A
Protected

Academic year: 2021

Share "COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

Term Test #2

COSC 1020 3.0 Introduction to Computer Science I Section A, Summer 2005

Family Name:

Given Name(s):

Student Number: | | | | | | | | | |

Question Out of Mark A Total 16

B-1 7

B-2 4

B-3 4

B-4 4

B Total 19

C-1 4

C-2 12

C-3 13

C Total 29

D-1 12

D-2 12

D-3 12

D Total 36 Total 100

Letter grade

(2)

Group A Questions <16 marks>

Read the following statements carefully. For each one, circle T if the statement is True and F if it is False. If you are not sure about a statement, leave it blank because a deduction of 1 mark will be applied for each incorrect answer.

T F 1. If an object is described as immutable, this means that certain of its attributes can be modified, but only through the use of proper mutator methods (and not through direct modification of the object’s fields).

FALSE — attributes cannot be modified in an immutable object T F 2. Instances of strings are mutable in Java.

FALSE — strings are immutable

T F 3. String is the only non-primitive type in Java that can be instantiated through the use of a literal.

TRUE

T F 4. Whenever a string literal appears, the Java compiler replaces it with a reference to a Stringobject that gets created implicitly.

TRUE — p. 220

T F 5. If neither of the operands of the + operator are instances of String, then both operands are type-coerced to strings.

FALSE — p. 222

T F 6. Setting String s = null; results in s.length() == 0.

FALSE — length undefined for a null reference

T F 7. The string Java matches the regular expression J+f?[va]*

TRUE — f is optional, any combination of a and v can follow one or more J

T F 8. In the design phase of a system’s development process, the needed classes are iden- tified and specified.

TRUE

(3)

T F 9. Through rigorous testing, one can prove the absence of errors in a software program.

FALSE — p. 261

T F 10. Black box testing requires access to a system’s source code.

FALSE — one needs only to be able to run the system T F 11. Every composition relationship is also an aggregation.

TRUE — by definition

T F 12. Class Dog has a private attribute of type Elephant. This means there is a composition relationship between these two classes.

FALSE — There might be other references to the Elephant objects

T F 13. A statically allocated collection must contain at least one static method.

FALSE — p. 307

T F 14. One cannot add an element to a collection, if it is already in it.

FALSE — only true for set collections

T F 15. If a search algorithm has a complexity of O(N), this means that if it takes 5 seconds to search through an input of size 5, it will take 5000 seconds to search through an input of size 5000.

FALSE — only works if both input sizes are large

T F 16. It is not possible to search through a collection of size N with an algorithm whose complexity is smaller than O(N).

FALSE — if the collection is organized in a smart fashion, it is possible

(4)

Group B Questions <19 marks>

Consider the following code segments. For each one, check the box next to the expected outcome. Follow the instructions given after the selected answer. You can assume all needed classes have been imported.

B–1. [7 marks]

int a = 45;

int b = 50;

System.out.println( a + b + " is the sum" );

System.out.println( "The sum is " + a + b );

Compiles and runs (write output in space below).

Compile-time error (explain source of error on code above).

Run-time error (explain source of error on code above).

Compiles and runs. Output is:

95 is the sum The sum is 4550

1 mark - Correct box checked

3 marks - Correct first line of output 3 marks - Correct second line of output B–2. [4 marks]

int a = 45;

int b = 50;

System.out.println("The difference is" + a - b);

Compiles and runs (write output in space below).

Compile-time error (explain source of error on code above).

Run-time error (explain source of error on code above).

Compile-time error; the ’-’ operator not defined for string operand 1 mark - Correct box checked

3 marks - Correct explanation

(5)

B–3. [4 marks]

int a = 4;

int b = 5;

System.out.println("The product is" + a * b);

Compiles and runs (write output in space below).

Compile-time error (explain source of error on code above).

Run-time error (explain source of error on code above).

Compiles and runs. Output is:

The product is20

1 mark - Correct box checked

3 marks - Correct output (no deduction if there is a space between ”is” and ”20”) B–4. [4 marks]

System.out.println(Integer.parseInt("5 ");

Compiles and runs (write output in space below).

Syntax error (explain source of error on code above).

Run-time error (explain source of error on code above).

Two different answers will be taked as correct due to an inadvertent typo:

Syntax error - a parenthesis is missing

Runtime error - parseInt throws exception when the string argument does not cor- respond to an integer.

4 marks only for one of these two combinations

(6)

Group C Questions <29 marks>

For this set of questions, you can assume that the attributes of all classes are public, and that none of the object references are null.

The UML diagrams of the following classes contain all their attributes.

Ant + i : int + b : Bear

Bear + d : double + c : Cat

Cat + l : long + f : float In the following, x and y are two references of type Ant.

C–1. [4 marks]Write a code fragment that will output true if and only if x is an alias of y. In all other cases, the output should be false.

output.println(x == y);

C–2. [12 marks] Write a code fragment that will output true if and only if the object referred to by x is a shallow copy of the object referred to by y. In all other cases, the output should be false.

boolean notAlias = x != y;

boolean samePrimitive = x.i == y.i;

boolean sameNonPrimitive = x.b == y.b;

output.println(notAlias && samePrimitive && sameNonPrimitive);

4 marks per boolean variable above. See note about equals() in next page.

(7)

C–3. [13 marks] Write a code fragment that will output true if and only if the object referred to by x is a deep copy of the object referred to by y. In all other cases, the output should be false.

boolean notAlias = x != y;

boolean samePrimitive = (x.i == y.i) && (x.b.d == y.b.d) &&

(x.b.c.l == y.b.c.l) && (x.b.c.f == y.b.c.f);

boolean sameNonPrimitive = (x.b != y.b) && (x.b.c != y.b.c);

output.println(notAlias && samePrimitive && sameNonPrimitive);

Note that notAlias is actually redundant, as well as the expression (x.b != y.b) in sameNonPrimitive.

6 marks for sameNonPrimitive

6 marks for samePrimitive (only second part necessary) 1 mark for the exact five boolean expressions.

Since the equals() method is not defined for these classes, it is equivalent to using

==. However, if the answer describes a different equals() method, then it will be accepted.

(8)

Group D Questions <36 marks>

D–1. [12 marks]

On the next page, write an app TT2 D1 that prompts for and reads a string from the user. The behaviour of the app should be as follows:

If the length of the inputted string is even, the program outputs the string after padding it with ’+’ characters at its two ends, so that the total number of ’+’ charac- ters is equal to the number of characters in the inputted string, and the number of ’+’

characters to the left of the inputted string is equal to the number of ’+’ characters to its right.

If the length of the inputted string is odd, the program outputs the string after padding it with ’*’ characters at its two ends, so that the total number of ’*’ char- acters is equal to twice the number of characters in the inputted string, and the num- ber of ’*’ characters to the left of the inputted string is equal to the number of ’*’

characters to its right.

Sample inputs and corresponding outputs are given in the table below.

Sample Input Desired Output

This is a test +++++++This is a test+++++++

This is a test! ***************This is a test!***************

Hi +Hi+

m *m*

Partial marks will be awarded for partially-correct solutions.

(9)

import java.io.PrintStream;

import java.util.Scanner;

public class TT2_D1 {

public static void main (String[] args) {

Scanner input = new Scanner(System.in);

PrintStream output = System.out;

output.println("Enter a string");

String s = input.nextLine();

String padding = "";

if ( s.length()%2 == 0 ) { int num = s.length()/2;

for (int i=0; i<num; i++ ) { padding+="+";

} }

else {

int num = s.length();

for (int i=0; i<num; i++ ) { padding+="*";

} }

output.println(padding+s+padding);

(10)

D–2. [12 marks]On the next page, complete the app TT2 D2, so that it outputs the num- ber of credit cards in gc with a negative balance. Excerpts from the API of classes GlocalCreditand CreditCard are shown on pages 13 and 14.

D–3. [12 marks] On page 12, write an app TT2 D3 that prompts for and reads a string from the user, and determines whether the inputted string is a palindrome. A palin- drome is a string that reads the same forwards and backwards. The output should be simply true or false (as the case may be). Assume that palindromes are case sensitive.

Sample inputs and corresponding outputs are given in the table below.

Sample Input Desired Output

madam true

Madam false

ahha true

aibohphobia true

a true

hello false

Partial marks will be awarded for partially-correct solutions.

(11)

import java.io.PrintStream;

import type.lib.*;

public class TT2_D2 {

public static void main(String[] args) {

PrintStream output = System.out;

GlobalCredit gc = GlobalCredit.getRandom(false);

int count = 0;

for (CreditCard cc = gc.getFirst();

cc != null ; cc = gc.getNext()) {

if (cc.getBalance() < 0) count++;

}

output.println(count);

} }

Only chained traversal is acceptable, since there is no get(int) method in GlobalCredit.

(12)

import java.io.PrintStream;

import java.util.Scanner;

public class TT2_D3 {

public static void main (String[] args) {

Scanner input = new Scanner(System.in);

PrintStream output = System.out;

output.println("Enter a string");

String s = input.nextLine();

int midPos = s.length()/ 2;

if (s.length()%2 != 0) midPos +=1;

boolean isPalindrome = true;

for (int i=0; i<midPos; i++) { boolean match =

s.charAt(i) == s.charAt(s.length()-1-i);

isPalindrome = isPalindrome && match;

}

output.println(isPalindrome);

(13)

Excerpts from the API of class GlobalCredit

This class encapsulates the credit card operations of a Global Credit Centre (GCC), a regional card processing centre of the Global Credit bank. It maintains a collection of credit cards and provides services for traversal and content addressing.

Method Detail

public CreditCard get(java.lang.String number)

Find the card whose number is passed.

Parameters:

number - the number of the card to find.

Returns:

a reference to the card whose number is passed. If the card is not found in the collection, or if the passed number is null, then null is returned.

public int size()

Determine the number of credit cards registered with this GCC.

Returns:

the number of credit cards that were added to this GCC.

public CreditCard getFirst()

The base invocation of the forward iterator.

Returns:

the first credit card issued. If none were issued, null is returned.

public CreditCard getNext()

Follow up invocation of the forward iterator.

Returns:

the next issued credit card. If no more cards are present, null is returned. It is assumed that getFirst is first invoked, and if it had a non-null return, then this method is invoked repeatedly until null is returned.

(14)

public static GlobalCredit getRandom(boolean same)

Create a randomly chosen GCC. The GCC has no-name and contains anywhere between 2 and 20 credit cards (the count is uniformly distributed between 2 and 20). One of the cards has the number

"123456-6"; otherwise, the cards have randomly chosen-numbers, random balances, random expiry dates, and "Random" as the cardholder name.

Parameters:

same - a flag that determines if a new GCC is to be generated (when false) or if a reference to the last-generated GCC should be returned (true). You should normally set it to false to get a different GCC per invocation.

Returns:

a GCC that is either randomly generated (if same is false) or is the same as the one last generated by this method. If this method was never invoked before, a random GCC is returned regardless of the passed parameter.

Excerpts from the API of class CreditCard

This class encapsulates a credit card and maintains information about it. Each card is identified by the attributes: card number, holder’s name, issue and expiry dates, credit limit, and balance owing.

Method Detail

public double getBalance()

Credit card balance accessor.

Returns:

the balance owing on this credit card.

public java.lang.String getNumber()

Credit card number accessor.

Returns:

the number of this credit card, which is a string of 8 characters.

References

Related documents

Enhanced antitumor immunity by targeting dendritic cells with tumor cell lysate-loaded chitosan

Most algorithms for large item sets are related to the Apri- ori algorithm that will be discussed in Chapter IV-A2. All algorithms and methods are usually based on the same

Make  changes  to  section  on  agreements  to   reflect  emphasis  of  agreements  in  new

3This result is explained by the fact that under Bertrand competition foreign investment in R&amp;D has a negative (indirect) strategic effect on foreign firms'

We show that intercellular interactions mediated by IL-2 and amplified by the extracellular positive feedback on IL-2R levels can account for both competition and co- operation

In Canada, the Canadian Wireless Telecommunications Association ( CWTA ) oversees mobile marketing. To help prevent mobile spam from occurring, there are strict policies in place

Even if the students are able to graduate high school and attend college, they are not likely to graduate with a degree.. “Latinos complete their bachelor’s degrees

7 The Exchange believes that it is appropriate to require Clearing Member approval before a market maker can reenter the market after the market-wide speed bump has been