You are on page 1of 10

Computer Programming

and
Introduction to Programming
Written Examination

9.7.2021

FIRST NAME LAST NAME


STUDENT NUMBER SIGNATURE

Academic Integrity Protocol

☐ Read and accepted

I acknowledge that University examinations require all students to respect the highest
standards of academic integrity. For the examination I am about to take, I make the
following pledge:
• All the work will be my own, and I will not plagiarize from any source;
• I will not obtain or seek to obtain an unfair advantage by communicating or
attempting to communicate with any other person during the examination; neither
will I give or attempt to give assistance to another student taking the examination;
• I will stop writing immediately at the designated end time of the examination and will
make no modification to my script thereafter.
• I understand that, if suspected of violating this pledge, I will be liable to be referred to
the Organ competent for disciplinary issues and may be subject to disciplinary action.
• I am also aware that voice- or videorecording of the online exams as well as the
dissemination of their contents is neither provided for nor allowed.
Reply to the following questions. You cannot consult any material.

1. Which of the following are legal Java identifiers?


A) i
B) class
C) ilikeclass!
D) idon'tlikeclass
E) i-like

Res: A and B

A) a
B) extends
C) classA!
D) Class-A
E) var

Res: A, B and E

A) object
B) extends
C) classA!
D) Class-A
E) var$

Res: A and B and E

2. For each of the following expressions, indicate the order in which the
operators will be evaluated by writing a number beneath each operator.

o a % b – (c * d) * e

1 4 2 3

o a + b * c / (d % e)

4 1 3 2

o a + b + c / d

1 3 2

---
o (a % b) - c * d * e

1 4 2 3
o (a + b) / c / d % e

1 2 3 4

o a + b / c * d

3 1 2

---
o a % b * c + d * e

1 2 4 3

o (a - b) * c / d * e

1 2 3 4

o a + b / (c * d) / e

4 2 1 3

3. Write the output of these statements:


int a, b = 0;
a = b++ + 1;
System.out.println(a);
a = 0;
a = --a + 2 + b;
System.out.println(a);

Res: 1, 2
int a = 0, b = 0;
a = b++ - --b;
System.out.println(a);
b = 0;
b = --a + 2*b;
System.out.println(b);

Res: 0,-1
int a = 0, b = 0;
a = b++ - 1;
System.out.println(a);
a = 0;
a = --a + a++ -b;
System.out.println(a);
Res: -1, -3

4. If the following code snippet is syntactically correct what it will be printed?

char total = 20;


float result = total / 6;
System.out.println(result);
Res: 3.0

char total = 10;


float result = total / 6;
System.out.println(result);

Res: 1.0

char total = 40;


float result = total / 6;
System.out.println(result);

Res: 6.0

5.
Write a method called randomInRange that accepts two integer parameters (m
and n) representing a range. Assume that the second parameter is greater than
the first. The method should return a random integer in the specified range
(inclusive), that is a number in the set {m, m+1, …, n}. Hint: use an object of
the class Random and the method nextInt(int bound) of the class Random (it
returns an int).

Res:
public static int randomInRange(int first, int second) {
Random generator = new Random();
int range = second - first + 1;
return generator.nextInt(range) + first;
}

Write a method called randomInRange that accepts two integer parameters (m


and n) representing a range. Assume that the first parameter is greater than the
second. The method should return a random integer in the specified range
(inclusive), that is a number in the set {n, n+1, …, m}. Hint: use an object of
the class Random and the method nextInt(int bound) of the class Random (it
returns an int).

Res:
public static int randomInRange(int first, int second) {
Random generator = new Random();
int range = first - second + 1;
return generator.nextInt(range) + second;
}

6.
Suppose you have a class called Movie. Write a constructor for the class that
initializes the title (String) and director (String) instance variables based on
parameters passed to the constructor.

Res:
public Movie(String theTitle, String theDirector) {
title = theTitle;
director = theDirector; }

Suppose you have a class called Student. Write a constructor for the class that
initializes the name (String) and age (int) instance variables based on
parameters passed to the constructor.
public Student(String theName, int theAge) {
name = theName;
age = theAge; }

7.
Consider the following code snippet. What value must have x so that the code
will print the output shown below:

int num = 1, max = 30;


while (num < max) {
System.out.println(num);
num += x*2;
}

1
9
17
25

Res x=4

int num = 1, max = 30;


while (num < max) {
System.out.println(num);
num += x*3;
}

1
7
13
19
25

Res x=2

int num = 1, max = 30;


while (num < max) {
System.out.println(num);
num += x*x;
}

1
5
9
13
17
21
25
29

Res x=2

8. Assume that the following class has been defined:

public class ExInheritance {

private static class Building implements Runnable {

public int getVolume() {


return 0;
}

public void run () {};


}

Can you add the following method to the class Building? Yes or No and explain the
reply.

public double getVolume() {


return 0.0;
}

RES: No because it has been already defined and it has the same signature as the
previous one.

9.
Transform the following while loop into an equivalent do loop (make sure it
produces the same output).

int n = 1;
while (n <= 10) {
n++;
System.out.println(n);
}

Res:

int n=1;
do {
n++;
System.out.println(n);
}
while (n<=10);

Transform the following while loop into an equivalent for loop (make sure it
produces the same output).

int n = 1;
while (n <= 10) {
n++;
System.out.println(n);
}

Res:

for (int n = 2; n <= 11; n++)


System.out.println(n);

10. Which instance data are accessible in the class A3?

public class A1 {
public int y;
private int x;
protected int z;
… }
public class A2 extends A1 {
protected int a1;
private int a2;
public int a3;
…}
public class A3 extends A2 {
private int b;
…}

Res: y, z, a1, a3, b

public class A1 {
public int a;
private int b;
protected int c;
… }
public class A2 extends A1 {
protected int d;
private int e;
public int f;
…}
public class A3 extends A2 {
private int g;
…}

Res: a, c, d, f, g

11. What output is produced by the following code fragment?

public static void main (String[] args) {


final int MAX = 10;
for (int r = 1; r <= MAX; r++) {
for (int t = 1; t <= r * 2; t = t + 2)
System.out.print("*");
System.out.println();
}
}
Res:
*
**
***
****
*****
******
*******
********
*********
**********

public static void main (String[] args) {


final int MAX = 10;
for (int r = 1; r <= MAX; r++) {
for (int t = 1; t <= r * 3; t = t + 3)
System.out.print("*");
System.out.println();
}
}

Res:
*
**
***
****
*****
******
*******
********
*********
**********

12. Discuss the manner in which Java passes parameters to a method. Is this
technique consistent between primitive types and objects? Explain.

Res: Java passes all parameters by value. This means that the current value of the
actual parameter is copied into the formal parameter in the method header. This
technique is consistent between primitive types and objects because object references
rather than objects themselves are passed. When an object (actually, an object
reference) is passed, the current value of the reference (the object's address) is copied
into the corresponding formal parameter in the method header.

13.
Write code that sets each element of a boolean array called flags to alternating
values (true at index 0, false at index 1, etc.).

Res:
boolean[] flags = new boolean[10];
for (int index = 0; index < flags.length; index++)
flags[index] = (index % 2 == 0);

Write code that sets each element of a boolean array called flags to alternating
values (false at index 0, true at index 1, etc.).

Res:
boolean[] flags = new boolean[10];
for (int index = 0; index < flags.length; index++)
flags[index] = (index % 2 != 0);

14. Write a static method that computes an approximation of the cosine function
by using the following series:

public static double cosine(double x, int k) {…}

This method will return the result of the computation of the series where the index n
ranges from 0 to k (not to infinite!). Assume that a static method fact(int n), which
returns the (int) factorial of n, is already declared in your code.

Sol

public static double cosine(double x, int n) {


double res = 0;
for (int i = 0; i <= n; i++)
res += Math.pow(-1, i) * Math.pow(x, 2 * i) / fact(2 * i);
return res;
}

15. Write a recursive method that implements the following recursive function:

F(0) = 0
F(1) = 1 

F(n) = F(n-1) + 2*F(n-2)

Res:

public static int recF (int n) {


if (n < 2)
return n;
else
return recF(n-1) + 2 * recF(n-2);
}

You might also like