INSTITUT TEKNOLOGI SEPULUH NOPEMBER
Faculty of Intelligent Electrical, Electronic, and Informatics Engineering
Department of Information Systems
Bachelor of Computer Science Program in Information Systems
SEMESTER II, MIDTERM EXAMINATION, 2024/2025
ES234211 – Programming Fundamental
Course Convenors
Aris Tjahyanto
Ahmad Muklason
Answer and Marking Scheme
7 June 2025 TIME ALLOWED: 2.5 HOURS
INSTRUCTIONS TO CANDIDATES
1. This examination paper contains SEVEN (7) questions
2. Answer strictly FIVE (5) questions only, i.e. questions numbers 1-4,
and one question of questions 5-7. The marks for each question are
indicated at the beginning of each question.
3. Answer the questions in any order.
4. This is CLOSED BOOK exam.
5. When justification is required, show the working steps concisely and
systematically. Do not provide only the final answer
6. Please work independently, and Good Luck!
ES234211
Question 1. (10 marks)
The following method checks whether the data is sorted in ascending order.
Example:
data | expected output
-----------------------------------------
{1,2,3,4} | The data is sorted
{6,8,8,3,1} | The data is not sorted
{2,2,5,6} | The data is sorted
{5,3,1,1} | The data is not sorted
Please complete the missing code.
static void sorting (int [] data){
____ sorted = ____;
for(int i=____;i<____;i++){
if(____){
sorted = false;
}
}
if(____){
System.out.println("The data is sorted");
} ____ {
System.out.println("The data is not sorted");
}
}
Answer
One of alternative answers:
static void sorting (int [] data){
boolean sorted = true;
for(int i=1;i<data.length;i++){
if(data[i-1]>data[i]){
sorted = false;
2
ES234211
}
}
if(sorted){
System.out.println("The data is sorted");
} else {
System.out.println("The data is not sorted");
}
}
Marking Scheme
There are seven blank spaces in total. Spaces 1, 2, 6, and 7 each account for
1 point, while spaces 3, 4, and 5 each account for 2 points, contributing to a
total score of 10.
Question 2. (20 marks)
Based on the flowchart shown in Figure 1, Answer the following questions.
Please note that <> means not equal.
(a) What is displayed on the screen according to the flowchart if NUM has
the value 10, 5, and 4? Explain each case.
(b) Explain the purpose of the flowchart.
(c) Provide a code snippet or program in Java.
Answer
(a.) Init: NUM = 10, REM = -1
NUM<>0 | REM = NUM%2 | OUPUT REM | NUM = NUM/2
----------------------------------------
T | 0 | 0 | 5
T | 1 | 01 | 2
T | 0 | 010 | 1
3
ES234211
T | 1 | 0101 | 0
F | |
Final Output:
0101Selesai
Similarly, for NUM=5, we get output:
101Selesai
Similarly, for NUM=4, we get output:
001Selesai
(b.) the purpose of the code is to convert decimal number to binary number
in reverse order.
(c.) import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int rem = -1;
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
while (num!=0){
rem = num%2;
System.out.print(rem);
num = num/2;
}
System.out.println("Selesai");
}
}
Marking Scheme
Part (a) account for 6 out of 20 points. Part (b) accounts for 4 out of 20
points. Part (c) accounts for 10 out of 20 points. Mark evenly for partially
correct answer.
4
ES234211
Question 3. (15 marks)
Suppose a method is defined as follow:
public static void mystery(int a, int b) {
int [] n = {1,3,5,7};
boolean [] k = {true,false,false,true};
String [] s = {"male","female"};
int sum = 1;
do{
int x = n[a%4];
boolean y = k[b%4];
String z = s[a%2];
sum = sum + (x+x/2-1);
if(!y && sum>x){
sum = sum+2;
}
System.out.print(sum+z+"#");
a = a+sum;
} while (a<b);
System.out.println();
}
What is the output from the following method call?Briefly justify your an-
swer!
(a) mystery(2,5)
(b) mystery(5,10)
Answer
(a.) First Iteration:
a = 2, b = 5, sum = 1
x = n[2 % 4] = n[2] = 5
y = k[5 % 4] = k[1] = false
z = s[2 % 2] = s[0] = "male"
5
ES234211
sum = sum + (x + x/2 - 1)
= 1 + (5 + 2 - 1) = 7
Since !y && sum > x is true (i.e., !false && 7 > 5),
add 2 to sum:
sum = 7 + 2 = 9
Output: 9male#
Update a:
a = a + sum = 2 + 9 = 11
Since a = 11 is not less than b = 5, the loop ends.
Final Output:
9male#
(b.) Initial values:
a = 5, b = 10, sum = 1
n = {1, 3, 5, 7}
k = {true, false, false, true}
s = {"male", "female"}
First Iteration:
x = n[5 % 4] = n[1] = 3
y = k[10 % 4] = k[2] = false
z = s[5 % 2] = s[1] = "female"
sum = sum + (x + x/2 - 1)
= 1 + (3 + 1 - 1) = 4
Since !y && sum > x is true (!false && 4 > 3), add 2:
sum = 4 + 2 = 6
Output: 6female#
a = a + sum = 5 + 6 = 11
Check condition: a < b → 11 < 10 → false, exit loop
Final Output:
6female#
6
ES234211
Marking Scheme
Part (a) account for 8 out of 15 points. Part (b) accounts for 7 out of 15
points. Mark evenly for partially correct answer. Halve the mark when
justification is not provided.
Question 4. (20 marks)
Complete the following method definition:
public static void stars(int n){
//your code goes here
In order to generate expected output with its respective method call as shown
below. Please note that your code has to work with parameter n, any integers
greater than 0.
Example:
n | expected output
-----------------------------------------
1 | *
-----------------------------------------
2 | *
| **
-----------------------------------------
3 | *
| **
| ***
-----------------------------------------
4 | *
| **
| ***
| ****
7
ES234211
Answer
One of alternative answers:
static void star(int n){
//Part (a)
for(int i=0;i<n;i++){
//Part (b)
for(int j=0;j<n-i;j++){
System.out.print(" ");
}
//Part (c)
for(int j=0;j<=i;j++){
System.out.print("*");
}
//Part (d)
System.out.println();
}
}
Marking Scheme
Part (a) - (c) each accounts for 6 out of 20 points. Part (d) accounts for 2
out of 20 points. Mark evenly for partially correct answer.
Question 5. (35 marks)
Complete the following method to convert binary number to decimal.
static void bin2Dec(String bitString){
//your code goes here
}
Example:
bitString | expected output
-----------------------------------------
1101 | 13
11001 | 25
101 | 5
8
ES234211
Answer
One of alternative answers:
static void bin2Dec(String bitString){
//Part (a)
int powerTwo = 1;
int i = 0;
int intVal =0;
int l = bitString.length();
//Part (b)
while(i<l){
//Part (c)
int v = Integer.parseInt(""+bitString.charAt(l-(i+1)));
//Part (d)
intVal += v*powerTwo;
//Part (e)
powerTwo *= 2;
i++;
}
//Part (f)
System.out.println(intVal);
}
Marking Scheme
Part (a) accounts for 4 out of 35 points. Part (b)-(e) each accounts for 7
out of 35 points. Part (f) accounts for 3 out of 35 points. Mark evenly for
partially correct answer.
Question 6. (35 marks)
Complete the following method to check whether a bit string is symmetric.
A bit string is symmetric if it reads the same forwards and backward
static void symBit(String bitString){
//your code goes here
9
ES234211
Example:
bitString | expected output
-----------------------------------------
1101 | No
1001 | Yes
101 | Yes
11100 | No
Answer
One of alternative answers:
static void symBit(String bitString){
//Part (a)
boolean sym = true;
int l = bitString.length();
//Part (b)
for(int i=0;i<l/2;i++){
//Part (c)
if(bitString.charAt(i)!=bitString.charAt(l-1-i)){
sym=false;
}
}
//Part (d)
if(sym){
System.out.println("Yes");
} else{
System.out.println("No");
}
}
Marking Scheme
Part (a) accounts for 8 out of 35 points. Part (b)-(c) each accounts for 10
out of 35 points. Part (d) accounts for 7 out of 35 points. Mark evenly for
partially correct answer.
10
ES234211
Question 7. (35 marks)
A robot starts at position (X, Y). It then moves Z steps, where each step
is in one of the following directions: right (r), left (l), up (u), or down (d).
Each move consists of 1 to 9 steps (inclusive). Write a complete Java program
namely Robot.java. to determine the robot’s new position. Your code should
read a single line file, named robot.txt with the following format:
input format: (robot.txt)
• The first and only line of input will contain 4 parts separated by space
• The first and second parts represent the current positions of robot, i.e.
X and Y respectively.
• The third part represents the number of moves, Z.
• The last part indicates the detail of moves, i.e. the direction and the
number of steps.
• The output is the new position of X and Y separated by space.
Sample Input 1:
3 6 4 l2r1u5d1
Sample expected Output 1:
2 10
Sample Input 2:
2 1 5 r2r3d6u7l1
Sample expected Output 2:
6 2
11
ES234211
Answer
One of alternative answers:
import java.util.Scanner;
public class Robot{
public static void main(String[] args) {
//Part (a)
Scanner rob = new Scanner(Robot.class.getResourceAsStream
("robot.txt"));
//Part (b)
int x = rob.nextInt();
int y = rob.nextInt();
int m = rob.nextInt();
String mov = rob.next();
//Part (c)
for(int i=0;i<mov.length();i+=2){
//Part (d)
if(mov.charAt(i)==’l’){
x-=Integer.parseInt(""+mov.charAt(i+1));
}
//Part (e)
else if(mov.charAt(i)==’r’){
x+=Integer.parseInt(""+mov.charAt(i+1));
}
//Part (f)
else if(mov.charAt(i)==’u’){
y+=Integer.parseInt(""+mov.charAt(i+1));
}
//Part (g)
else if(mov.charAt(i)==’d’){
y-=Integer.parseInt(""+mov.charAt(i+1));
}
}
//Part (h)
System.out.println(x+" "+y);
}
}
12
ES234211
Marking Scheme
Part (a)-(b) accounts for 4 out of 35 points. Part (c)-(g) each accounts for 5
out of 35 points. Part (h) accounts for 2 out of 35 points. Mark evenly for
partially correct answer.
13
ES234211
Cheat Sheet
String Methods:
-----------------------------------------------------------
return type | method and description
-----------------------------------------------------------
String[] | split(String regex)
| Splits this string around
| matches of the given regular expression.
-----------------------------------------------------------
char | charAt(int index)
| Returns the char value at the specified index.
-----------------------------------------------------------
Scanner Methods:
-----------------------------------------------------------
String | next()
| Finds and returns the next complete token
| from this scanner
-----------------------------------------------------------
int | nextInt()
| Scans the next token of the input as an int.
-----------------------------------------------------------
String | nextLine()
| Advances this scanner past the current line and
| returns the input that was skipped.
-----------------------------------------------------------
Integer Methods:
-----------------------------------------------------------
int | parseInt(String s)
| Parses the string argument as
| a signed decimal integer.
-----------------------------------------------------------
14
ES234211
Figure 1: Flowchart for Question 2
15