PPL Final Test Solution
PPL Final Test Solution
SPRING 2014
2c12
Hanoi University
Part I: multiple choice questions
Question 1
int x = 0;
int y = 0;
x = x + 2 * y;
y++;
int z = x/y ;
a. 100, 10, 10
b. 90, 10, 9
c. 70, 10, 3
d. run-time error
Đáp án : 1 b
Question 2
Which of the followings is the output of this code?
if (j % 5 == i) {
System.out.printf(“%3d”, j);
System.out.prinln();
a.
01234
56789
10 11 12 13 14
15 16 17 18 19
b.
012345
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
c.
0 5 10 15
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
d. run-time error
Đáp án 2 c
Question 3
if (c == string.charAt(i)) {
System.out.printf(“%-2s”, c);
break;
a. b e n o r t
b. T r o n e b t
c. T o b e r n t
d. run-time error
Đáp án 3a
Specify and implement a procedure named frequencyIf, which computes and returns an
array containing the numbers of the occurrences of the elements of an integer array that satisfy
a given condition. The condition must be a string of the form op val where op is one of the
followings operators: <=, >=, |, val is an integer value. The operator | means “divisible by”.
Frequencies of the elements that do not satisfy the condition are set to 0. For example:
Đáp án :
int k = 1;
if(i==j) continue;
else{
if(a[i]==a[j]){
k+=1;
b[i] = k;
else if(a[i]!=a[j]){
b[i] = k;
return b;
Given below is the specification and Java code of a procedure. Add suitable mid-conditions to
the code and use these to determine the loop invariant. Write the invariant in the block
comment that is marked in the code.
/**
*/
public static int method1(int[] a){
int n = a[0];
/*
* Loop invariant:
*/
int x;
x = a[i];
if (x > n){
n = x;
return n;
Đáp án :
/**
int n = a[0];
/*
* Loop invariant: *
*/
int x;
x = a[i];
// x = a[i];
if (x > n){
n = x;
Given below is a listing of partial design specification of a class named IntegerArray. Given
also that the statement import at the top imports the annotation class DomainConstraint
for use in this class. Answer the following questions to complete the design and code of this
class. The TODO block comments in the listing mark the regions where you need to write your
answers.
import userlib.DomainConstraint;
/**
* @attributes
* array Integer[]
* len Integer
*
* @object
* @abstract_properties
* mutable(array)=true /\ optional(array)=false /\
* length(array) = len
* @rep_invariant
* array != null /\
* len >= 1 /\
* array.length = len
*/
/**
* TODO: ATTRIBUTES
*/
/**
*/
/**
*/
/**
*/
/**
* @effects
*/
} // end IntegerArray
Đáp án : bài này mình làm gộp vào 1 class code cho tiện
import userlib.DomainConstraint;
/**
* @attributes
* array Integer[]
* len Integer
* @object
* A typical IntegerArray is [x1,x2, ..., xn], where xi is integer for all 1 <= i <= n.
* @abstract_properties
* mutable(array)=true /\ optional(array)=false /\
* length(array) = len
* @rep_invariant
* array != null /\
* len >= 1 /\
* array.length = len
*/
@DomainConstraint(type="int[]",mutable=true,optional=false)
private int[] array;
@DomainConstraint(type="int",mutable=false,optional=false,min=1)
private IntegerArray(){
//do nothing
this.array = array;
len = array.length;
return array;
public getLength(){
return len;
}
/**
*/
/**
* @effects
*/
int n = 0;
n += array[i];
return n;
} // end IntegerArray
a) What is the state of the program in the stack and heap memories when the code is run?
int i, j;
System.out.println(x[i]);
Đáp án :
a.
x 10 20 30
y 2468
i 0 x[0] = 10 + 2 + 4 + 6 + 8 = 30
j 0123
i 1 x[1] = 20 + 2 + 4 + 6 + 8 = 40
j 0123
i 2 x[2] = 30 + 2 + 4 + 6 + 8 = 50
j 0123
b.
Console output :
30
40
50
( có xuống dòng )
Given below is the partial definition of a class named Module. Briefly discuss the design issues
concerning the three attributes of this class and the solutions for them.
import java.util.Vector;
/**
*/
Đáp án :
import java.util.Vector;
/**
*/