CS102 2122 Fall Midterm Sol
CS102 2122 Fall Midterm Sol
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
}
(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);
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);
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
Or:
C102 - Midterm 4