0% found this document useful (0 votes)
8 views

CS102 1819 Spring Midterm Sol

The document is a midterm exam key for a CS 102 course covering Java programming concepts. It includes solutions for tracing Java program outputs, array manipulation, character swapping in strings, and reading from a text file to compute statistics. Each question has specific points assigned and demonstrates various programming tasks and methods.

Uploaded by

ysfelmci
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CS102 1819 Spring Midterm Sol

The document is a midterm exam key for a CS 102 course covering Java programming concepts. It includes solutions for tracing Java program outputs, array manipulation, character swapping in strings, and reading from a text file to compute statistics. Each question has specific points assigned and demonstrates various programming tasks and methods.

Uploaded by

ysfelmci
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

KEY

CS 102 - Introduction to Programming II (Java)


Midterm - 2019 Spring (01 April 2019)
Student ID: Name Surname:

Q1 (15 p) Q2 (25 p) Q3 (30 p) Q4 (30 p) Total (100)

Question 1 (15 points: 5 + 5 + 5)


Trace the following Java program segments and clearly write their output. Do not erase
k
the trace values from this paper.
1
(a) Write the output of this Java program segment: 4
17
String str = "Peace on Earth, Peace in Universe"; 20
int k = str.indexOf('e'); 29
while(k >= 0) 32
{ -1
if (k >= 2)
System.out.print(str.substring(k-2, k+1) + ":");
k = str.indexOf('e', k + 1); // returns index of 'e' after position k+1
}

Output: ace: Pe:ace:ive:rse:

(b) Write the contents of nums2 array after this program segment is executed. idx remain j
0 0 0
int[] nums = {71, 20, 8, 16, 7, 10}; 1
int[] nums2 = new int [nums.length]; 1 2
int idx = 0; 2 3
for (int remain = 0; remain < 2; remain++) 3 4
{ 5
for (int j = 0; j < nums.length; j++)
{
4 6
if (nums[j] % 2 == remain) 1 0
{ 5 1
nums2[idx] = nums[j]; 2
idx++; 3
} 4
} 6 5
} 6
2
Contents of nums2:
20 8 16 10 71 7

(Bonus + 5 points) Explain in english the task performed in (b) by the statements starting from the
statement: int idx = 0;
This program segments fills the array nums2 first by taking the even
values in nums, and then by taking the odd values in nums.
C102 - Midterm 1 01 April 2019
KEY
(c) Output:
int k = 20; 024681012141618
do
{ 02468101214
for (int j = 0; j < k; j += 2) 0246810
System.out.print (j);
0246
System.out.println ();
k -= 4; 02
} while (k > 0);

Question 2 (25 points)


Write a static method (moveLastToN) that gets an array of integers and a valid index number (lets say
n) as parameters, moves the last element to index position n and the elements between one position
down. The method will not create another array or an ArrayList, will do changes in the given array.
Example:
int[] arr = {8, 4, 7, 2, 6, 5, 1, 10, 9, 3};
moveLastToN (arr, 4);
will result as the array arr to contain: 8 4 7 2 3 6 5 1 10 9

// Moves the last element in an array to a given index position (n).


// Shifts the element in between as necessary.
private static void moveLastToN(int[] arr, int n)
{
int save = arr[arr.length - 1]; // Save last element. // 4 p
for (int j = arr.length - 1; j > n ; j--) // 12 p
{
arr[j] = arr[j-1]; // Move elements between // 5 p
}
arr[n] = save; // Move last element to pos n // 4 p
} // if } is missing, -1p

C102 - Midterm 2 01 April 2019


KEY
Question 3 (30 points)
Write a static method (swapCharPairs) that gets a string as parameter, and returns a string made as
follows: the characters at positions 0 and 1 are swapped, at positions 2 and 3 are swapped, etc... until
the end of string. If string has odd number of characters, then the last character will remain at its end
position. ATTENTION: The method must not change the parameter string, it shall return a new string.
Examine the example below.
Sample method call:
String aaa = "Abcde 12345";
String bbb = swapCharPairs (aaa);
System.out.println("<" + bbb + ">"); // <bAdc e21435>

Hint: To concatenate (append) a character to end of a string:


char c = str1.charAt(j); // get char at position j in str1
str2 = str2 + c; // append c to end of str2.

// Swap char at positions 0 and 1, then 2 and 3, then 4 and 5,


// till the end of string. If string has odd number of characters,
// then the last character will remain at its last position.
private static String swapCharPairs(String str)
{
int len = str.length(); // length of string
String str2 = ""; // initialize result string // 4 p
for (int j = 0; j < len - 1; j += 2) // 8 p
{
str2 = str2 + str.charAt(j+1) + str.charAt(j); // 4p
}
// If string has odd number of characters, append last character:
if (len % 2 == 1) // 4 p
{
str2 = str2 + str.charAt(len - 1); // 4 p
}
return str2; // 4 p
} // end swapCharPairs // if } is missing, -1 p

C102 - Midterm 3 01 April 2019


KEY
Question 4 (30 points)
A text file "grades.txt" contains an unknown number of double numbers (that are overall grades of
students in a course) and the values of these numbers are between 0.0 and 100.0.
Write a Java program that reads the double numbers from the text file "grades.txt", finds and prints
the number of grades, the sum, the average, the maximum and the minimum of them.
Sample output:
Number of grades: 88
Sum = 4568.97 Average = 51.92
Max = 98.81 Min = 9.29

public class Q4
{
public static void main (String[] args) throws IOException
{
// Open file:
Scanner fileScan = new Scanner (new File("grades.txt"));
int numInt = 0;
double sum = 0, max = 0, min = 100;
// While there is something in the file, read it.
while (fileScan.hasNext())
{
double n = fileScan.nextDouble(); // Read next double.
numInt++; // Increment counter.
sum += n; // Add to sum
if (n > max) // Check for max
max = n;
if (n < min) // Check for min
min = n;
}
fileScan.close(); // Close file stream.
double ave = sum / numInt; // Compute average
System.out.println ("Number of grades: " + numInt);
// System.out.printf ("Number of grades: %d\n", numInt);
// System.out.println ("Sum = " + sum + " Average = " + ave);
System.out.printf ("Sum = %.2f Average = %.2f\n", sum, ave);
System.out.println ("Max = " + max + " Min = " + min);
// System.out.printf ("Max = %.2f Min = %.2f\n", max, min);
}
}

C102 - Midterm 4 01 April 2019

You might also like