0% found this document useful (0 votes)
13 views4 pages

CS102 2122 Fall Midterm Sol

This document is a midterm exam key for a CS 102 course covering Java programming. It includes multiple-choice questions on Java syntax, output tracing of Java code segments, and programming tasks requiring the implementation of specific methods. The exam assesses students' understanding of loops, string manipulation, and method creation in Java.

Uploaded by

ysfelmci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

CS102 2122 Fall Midterm Sol

This document is a midterm exam key for a CS 102 course covering Java programming. It includes multiple-choice questions on Java syntax, output tracing of Java code segments, and programming tasks requiring the implementation of specific methods. The exam assesses students' understanding of loops, string manipulation, and method creation in Java.

Uploaded by

ysfelmci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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 - 2021 Fall (08 November 2021)
Student ID: Name Surname:

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

Question 1 (25 points: 5 points each)


Circle the letter of correct answer.
1.1. n and k are integer variables that contain positive integer numbers.
Which for statement is correct to generate all positive integer numbers that start from k and
decrease by n every time? I.e.: the for loop will repeat for k, (k-n), (k-2n), ... until 0.
a. for (int j = k; j > 0; j--)
b. for (int j = k; j > n; j -= n)
c. for (int j = k; j > 0; j -= n)
d. for (int j = 1; j < k; j += n)
e. None of the others

1.2. Which for statement is correct to generate all negative two-digit integers in increasing order?
I.e.: the for loop will repeat for -99 -98 -97 ... -10.
a. for (int j = -99; j < -10; j++)
b. for (int j = -10; j > -100; j++)
c. for (int j = -99; j < -10; j--)
d. for (int j = -99; j <= -10; j++)
e. None of the others

1.3. Which Java statement creates str2 String object containing the last 4 characters of String str.
a. String str2 = str.substring(str.length() - 4);
b. String str2 = str.charAt(str.length() - 4, str.length());
c. String str2 = str.substring(-4);
d. String str2 = str.substring(str.length() - 5);
e. String str2 = str.substring(len(str) - 4);

1.4. s1 and s2 are string objects. Which Java statement can be used to get the index of the first
occurence of s2 in the second half of s1?
Consider the following strings:
String s1 = "01234012340123401234";
String s2 = "12";
a. int k = s1.indexOf(s2, s1.length() / 2);
b. int k = s1.indexOf(s2);
c. int k = s1.indexOf(s2, length() / 2);
d. int k = s1.indexOf(s2, s1.length / 2);
e. None of the others

1.5. Which Java statement converts only the first character of a String hh to uppercase without
changing the other characters?
a. hh = Character.toUpperCase(hh.charAt(0)) + hh[1:hh.length()];
b. hh = hh.toUpperCase();
c. hh = Character.toUpperCase(hh.charAt(0)) + hh.substring(2);
d. hh = hh.substring(0, 1).toUpperCase() + hh.substring(1);
e. None of the others

C102 - Midterm 1
KEY
Question 2 (25 points: 10 + 10 + 5)
Trace the following Java program segments and clearly write their output. Do not erase
the trace values from this paper. k
(a) Write the output of this Java program segment: 1
4
String str = "Peace on Earth, Peace in Universe"; 17
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' starting from index k+1
}

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


(only output,
nothing else)

(b) What is printed by the following Java statements? Output: (write only the output in the box)
024681012141618
int k = 20;
02468101214
do
{ 0246810
for (int j = 0; j < k; j += 2) 0246
System.out.print (j); 02
System.out.println ();
k -= 4;
} while (k > 0);

(c) What is printed by the following Java statement? Output:


System.out.println(method1(1234));
24
The method is defined as:
private static int method1(int n)
{ Write only the output in
int v = 1; the box, nothing else!
while(n > 0)
{
v *= n % 10;
n /= 10;
}
return v;
}

C102 - Midterm 2
KEY
Question 3 (25 points)
Write a static method aveDiv() that gets three integer numbers as parameters, and returns the
average (as double) of numbers divisible by 3rd parameter from the 1st parameter to 2nd parameter
both inclusive.
Example method call:
double x = aveDiv(200, 800, 49);

// Returns the average (as double) of numbers divisible


// by 3rd parameter from the 1st parameter to 2nd parameter
// both inclusive.
private static double aveDiv(int n1, int n2, int n) // 4
{
int sum = 0, count = 0; // 2 p
for (int j = n1; j <= n2 ; j++) // 3 p
{
if (j % n == 0) // 3 p
{
count++; // 2 p
sum += j; // 2 p
}
}
return (double) sum / count; // 4 p
}

C102 - Midterm 3
KEY
Question 4 (25 points)
Write a static method moveChar() that gets a string, and an index value as parameters, and returns a
string made from 1st parameter such that the character with given index is moved to the beginning of
the string. Observe the example below.
Assume that the 2nd parameter contains a valid index value for the string in the 1st parameter.
ATTENTION: The method must not change the parameter string, it shall return a new string.
Examine the example below:
String aaa = "Abcd 12345";
String bbb = moveChar (aaa, 6); // char idx 6 moved to start
System.out.println(bbb); // 2Abcd 1345

Hint-1: Part of a string can be obtained by using the substring() method.


s2 = s1.substring(k, n); // Get substring from idx k to n-1.
Hint-2: To concatenate a character and a string:
char c = str1.charAt(n); // get char at position n in str1
str2 = c + str2; // append c and str2.

// Returns a string made from 1st parameter such that the


// character with given index is moved to the beginning of
// the string.
private static String moveChar(String s, int n) // 8
{
String s2 = s.charAt(n) + // 5
s.substring(0, n) + // 5
s.substring(n+1); // 5
return s2; // 2
} // end moveChar

Or:

private static String moveChar(String s, int n) // 8


{
return s.charAt(n) + s.substring(0, n) +
s.substring(n+1); // 17
} // end moveChar

C102 - Midterm 4

You might also like