COMP103 Mid Semester Test

advertisement
Name:
______________________________________
Name
Group Number: _______________________________
ID Number:
________________________________
SFDV2003 Mid Semester Test
Programming 1
Time allowed: 90 minutes (1.5 hour)
Worth: 15% of your final mark.
[Date]
Please fill in your name, your Group Number and your student ID number
Read the paper carefully before you start.
For multi choice questions select what you think is the single best answer (A..E).
If you make a mistake, erase the old mark completely. Each multiple choice question is worth one
mark. No marks will be given for questions with more than one selected answer.
For short answer questions fill in the answers in the spaces provided with the questions in this
script. The values of the questions are indicated in the margin.
Candidates may bring a single A4 sheet of notes (both sides) into the exam as reference material
(no magnifying devices are allowed). Candidates may not bring in any other written material,
calculators or electronic devices.
This paper consists of
pages, including this one.
Please check that you have a complete set.
Multi-choice Section.
1.
Java is:
(A)
(B)
(C)
(D)
(E)
2.
An objectionable language.
An objective language.
An object oriented language.
An organic language.
An obtusely angular language.
Objects can be characterised in terms of the values stored in their data fields, and the
operations performed by their methods. The usual terms for these characteristics are:
(A)
(B)
(C)
(D)
(E)
State and behaviour.
Data hiding and encapsulation.
Accessors and mutators.
Polymorphism and inheritance.
Greed and aggression.
1
3.
Which of the following is false?
(A)
(B)
(C)
(D)
(E)
4.
Which of the following statements are true?
w
x
y
z
(A)
(B)
(C)
(D)
(E)
5.
Data fields must be initialised when they are declared.
The inputs that a method expects are specified using formal parameters.
Java has reserved words which cannot be used as identifiers for variables.
Data fields are part of the class and can be accessed by any method in the class.
A public class named Hello, must be stored in a file called Hello.java.
The order that methods are declared in a class is not important.
We can specify the visibility of methods but not data fields.
Two or more methods declared in a class can have the same signature.
Two or more methods declared in a class can have the same name.
w and x
w and y
x and y
x and z
w and z
Which statement about the following method declaration is true?
public boolean absEqual(int in1, int in2) {// body }
(A)
(B)
(C)
(D)
(E)
6.
Which of the following declarations does not involve a primitive data type?
(A)
(B)
(C)
(D)
(E)
7.
boolean is the method name and the method returns an equal result.
The method returns two results of type int.
in1 and in2 are formal parameters and the return type is boolean.
The return type is public and the method has two formal parameters.
None of the above.
double x;
int a, b;
char c = 'x';
boolean b = true;
String s = "Happy Birthday";
Assume that a and b are variables containing double values. Which of the following
expressions correctly computes the product of a and b and then converts the sum to an
int (dropping the part after the decimal point)?
(A)
(B)
(C)
(D)
(E)
(int) (a * b);
(a * b) int;
(int) a + (int) b;
(int) a * (int) b;
int (a * b);
2
8.
Assume that a is a variable referring to an object of type MyClass. What will be
printed by the following statement?
System.out.println( a );
(A)
(B)
(C)
(D)
(E)
9.
'a'
"MyClass"
"AE903@MyClass"
Whatever string is returned by the toString method in MyClass.
Nothing, only the values of primitive variables can be printed in this way.
Assuming a boolean variable test and an integer variable x, which of the following
sets the value of test to the same value as the code below?
if (x == 0) test = true;
else test = false;
(A)
(B)
(C)
(D)
(E)
10.
test
test
test
test
test
=
=
=
=
=
x == 0;
if (x == 0);
true if (x == 0);
x > 0 else x < 0;
true if (x == 0) else false;
If n has the value 4, what is the value of sum after the following code has been executed?
int sum = 10;
for (int c = 0; c <= n; c++) {
sum += c;
}
(A)
(B)
(C)
(D)
(E)
11.
10
14
18
20
26
Which of the following is not true of a counter controlled loop?
(A)
(B)
(C)
(D)
(E)
The counter variable must be initialised.
The counter variable must be updated.
The counter variable must be declared.
The counter variable must be promoted.
The counter variable must be tested in the loop condition.
3
Short Answer Section
Please put your ID number on the top of short answer pages, and write answers on this script.
Question 21
For each part (and subpart) of this question assume the following initial declarations of variables.
int i = 8;
double d = 3.9;
char c = 'x', k;
(a)
(b)
In the assignment statements below, show the value stored in the left hand side variable of the
assignment statement (or write "Illegal" if the assignment statement is illegal):
(4)
i = 13 % 5;
__________________________
i += 4;
__________________________
i = (int) d;
__________________________
d = 3.14e3;
__________________________
d = 3 + 8 / 3;
__________________________
d = Math.sqrt(16.0);
__________________________
k = c;
__________________________
k = 'c';
__________________________
Write boolean assignment statements that set b to true when (and only when)
(2)
i is greater than 20 and less than 50
i is an odd number greater than 0
c is not 'i' or 'o' or 'u'
d is greater than 11.5 or less than 2.5
Question 23
(a)
Write a single statement which declares and initialises 2 variables, initialising their data
fields to 5 and 8.
4
(1)
(b)
(c)
(d)
Write a method called calc that takes two double input parameters and returns 10 times
their sum.
(2)
Assume that calc is in a class which has an double data field called cost. Rewrite the
method so that it returns nothing, but instead stores the result in the data field.
(1)
Assume that calc is in a class which has double data fields called a and b. Rewrite the
method so that it takes no input, but returns the sum of the data fields multiplied by 10.
(1)
5
Question 24
(a)
Show the output of the following code.
(2)
for (int i = 2; i <= 8; i++) {
if (i % 2 != 0)
System.out.println(i + i);
else
System.out.println(i * i);
}
(b)
Assume an int variable temp (with an unknown value) and a String variable status.
Write an if..else..if structure that assigns a value to status based on the first condition that
temp meets as follows:
temp less than 35, status is "Too cold"
temp less than 50, status is "Just right"
temp less than 70, status is "Too hot"
otherwise status is "Dangerously hot"
(2)
6
(c)
(d)
Write a while loop (including any declarations necessary) which will print out the
integers from 15 to 30 (inclusive).
What output is created by the following code?
(2)
(2)
for (int i = 3; i < 6; i++) {
System.out.println(i);
for (int b = 3; b <= i; b++) {
System.out.print(b);
//note print not println
}
System.out.println();
}
Question 25
(a)
The following method uses four double variables: sum, n, double and average.
public static void main(String [] args) {
double sum = 50.0, n = 5.0;
if (sum > 100.0) {
double twice = 2*sum;
7
(3)
System.out.println();
} else {
// Print 1
double average = sum / n;
System.out.println();
}
System.out.println();
// Print 2
// Print 3
}
The three println statements, which do not currently print anything, are labeled Print 1, Print
2 and Print 3. For each of these println statements show which of the four variables could be
printed (i.e. are in scope) at that point.
Print 1
Print 2
Print 3
(b)
Assume an application class with the methods below, and a worker class AClass with an
(2)
int data field that is set by a constructor. When the program runs, the first three statements
in main create three instance objects as briefly shown below. When the program reaches the
point of printing the message "Hello", the variables w, x, y, and z refer to various objects.
Show the correct references at this point by adding arrows from the variables to the objects.
public static void
AClass w = new
AClass x = new
AClass y = new
x = y;
fred(x);
main(String [] args) {
AClass(1);
AClass(2);
Variables
AClass(3);
w
Objects
1
x
}
y
public static void fred (AClass z) {
System.out.println("Hello");
}
8
2
z
3
Download