Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSC120 Spring 2022 Midterm

Mitsunori Ogihara

You have 75 minutes to complete the exam.

All   the   answers,    except    where   the    expected   answer    is   a    format   string    for System.out.printf. For example, instead of writing a = b, you can write a=b.

1. [Start] (1 point)     While taking the exam, you must work yourself.  No consultation with web resources or other people is permitted. You must use the Respondus browser in taking the exam.

In lieu of providing your signature, write yes in the box to indicate that you promise to follow the rules.

2. [Basics] (1 point each; 15 points total)    This section is about Java basics.

(a)  “Method  [A]” is the concept that a class may contain multiple methods having the same name as long as they have unique parameter [B] sequences.

(b)  The key word  [C], when attached to a variable,  prohibits reassignment to the variable.  Such a variable is called a [D].

(c)  There are four primitive data types representing whole numbers.  In the increasing order of bit length, the four are  [E],  [F],  [G], and  [H]. There are two primitive data types representing real numbers, [I] and [J], where the latter has more bits than the former.

(d)  To generate a random int value between 3 and 10 (8 possible values), we can use

the formula  [K] + [L](Math.[M] * [N]).

(e)  The value of Math.ceil(3.5) is  [O].

Answers

(a)  A. overloading, B. type

(b)  C. final, D. constant

(c)  E. byte, F. short, G. int, H. long, I. float, J. double

(d)  K. 3  (not 3.0), L.  (int), M. random(), N. 8  (8.0)

(e)  O. 4.0

3. [Switch] (18 points) The following code (presented with line numbers) is expected to examine the value of a String data x and produces an output.

01: String cls = "";

02: switch ( x ) {

03:  case "BIL":

04:   case "CSC": cls = "NatSci"; break;

05:  case "MLL":

06:   case "ENG": cls = "ArtHum"; break;

07:   default: cls = "Other";

08: }

09: System.out.print( "The classification is " + cls + "." );

10: System.out.println();

(a)  (2 points)  State a String literal equal to the value of cls after completing the execution of the code when x is equal to  "".

(b)  (2  points)  If we  move  Line  07  between  Line  02  and  Line  03,  will  the  output produced in Line 09 change? Answer yes or no.

(c)  (2 points each; 12 points total)

Suppose we rewrite the code as follows:

01: String cls = [A];

02: if ( [B] || [C] ) {

03:  cls = "NatSci";

04: }

05: else if ( [D] || [E] ) {

06:  cls = "ArtHum";

07: }

08: System.out.printf( [F], cls );

09: System.out.println();

Answer what [A] ..  [F] must be to preserve the behavior of the program.

(d)  (2 points)  Can the else if in Line 05 be changed to if without changing the behavior of the code in terms of the value of cls at the end?  Answer yes or no.

Answers

(a)  "Other"

(b)  yes

(c)  A.       "Other",       B.      x.equals( "BIL" ),        C.      x.equals( "CSC" ),        D.

x.equals( "MLL" ),

E. x.equals( "ENG" ),  F.  "The classification is %s.";  (d) yes

4. [Substring] (32 points total)

Recall the two-parameter version of the String method substring.  Suppose we are to develop its static version,  substr, that takes three parameters.  The first one is the string to which we will apply the method and the next two are the parameters for the method substring.  In other words, we want substr( w, p, q ) to behave like w.substring( p, q ).

(a)  (1 point each; 6 points total) The structure of the program will look like:

public [A] [B] [C]([D] w, [E] p, [F] q) {

// ...

}

Answer the words to placed in [A], ..., [F].

(b)  (2 points each; 24 points total) Continuing on the previous question, we can use a StringBuilder object and a for-loop to collect characters. The code will look like:

StringBuilder contents = [A]  [B]();

int i;

[C] ch;

for ([D] ; [E] < [F] ; [G]) {

ch = w.[H]([I]);

contents.[J](ch);

}

return([K].[L]);

The iteration variable of the for-loop must be i.  Answer what should be in these blanks.

(c)  (2  points) A run-time error that occurs often when accessing a  String data is StringIndexOutOfBoundsException.  Does the run-time error occur with the in- vocation of substr( "csc120", 100, 50 )?  Answer yes or no.

Answers

(a)  A.  static, B. String, C. substr, D. String, E. int, F. int

(b)  A. new, B. StringBuilder,  C. char, D. i=p, E. i, F. q,

G. i++ (i+=1, i=i+1, ++i, i=1+i),

H. charAt, I. i, J. append, K. contents, L. toString()

(c)  no

5. [Binary Representation] (32 points)    This part of questions is about binary repre- sentations.

(a)  (1 point) State the binary representation of the decimal number 19. Your answer shall have no leading 0’s.

(b)  (1 point) Does the representation have how many 1’s?

(c)  (3 points each; 30 points total) Suppose we want to compute the number of 1’s appearing in the binary representation of a given nonnegative long integer.  For example, the binary number 110 is the decimal number 6 and its number of 1’s is 2.

Suppose     that     we     have     already     written     a     public     static     method longToBinary( long m ) that  receives  as  a  parameter  long  integer  n and returns  its  binary  representation  as  a  String.     Using  longToBinary,  we  can calculate  the  number  of  1’s  in  a  variable  k as  follows,  where  m is  the  long nonnegative integer, given as input:

01: int k = 0;

02: String bin = [A];

03: for ( int i = [B]; [C]; [D] ) {

04:  if ( [E] ) { [F] };

05: }

06: System.out.println( "The number of 1s is " + k + "." );

Suppose we are to write a method  count that does the counting task without relying on the method  longToBinary.   The method will  dynamically compute the binary representation of the long integer m and instead of constructing the representation, just compute how many times a  1 is generated as the bit.  The program should preserve the value of m.

01: long n = m;

02: int k = 0;

03: while ( [G] ) {

04:  if ( [H] ) { [I]; }

05:  n = [J];

06: }

07: System.out.println( "The number of 1s is " + k + "." );

Answer what should be in [A] ..  [J].

Answers

(a)  10011

(b)  3

(c) A. longTBinary(m), B. 0, C. i=1, H. n%2==1, I. k++ (1+k), J. n/2

6. [Sequence] (24 points total)    Suppose we are to write a program that receives, from the user, a series of strictly positive numbers and that are strictly increasing (a new number must be greater than the previous number).  During the loop, the program receives a new number input, terminates the loop of the input fails to satisfy either condition  (it must be strictly positive and must be greater than the previous value prev), and then update the count k and the previous value.  Checking the increasing- ness condition is unnecessary for the very first input, but setting the value of prev to 0 in Line 03 makes it work even for the first input.

(a)  (2 points each; 22 points total)    Fill in the blanks of the code. 01: [A] keyboard = [B] [C]([D]);

02: int k = [E], input;

03: int prev = 0; // selecting the ideal initial value

04: while ( keyboard.[F] ) {

05:   input = keyboard.[G];

06:   if ( input  [H] ) break; // strict positiveness testing

07:   if ( input  [I] ) break; // increasing property testing

08:   [J]; // updating prev

09:   [K]; // updating k

10: }

11: System.out.println( "You've entered " + k + " numbers." );

Here is an execution example.

9

17

25

18

You've entered 3 numbers.

(b)  (2 points)    Will removing Line 06 change the behavior of the program?  Answer yes or no.

Answers

(a)  A.  Scanner, B. new, C. Scanner, D. System.in, E. 0,

F. hasNext() (hasNextInt()), G. nextInt(), H. <=0, I. <=prev, J. prev=input, K. k++ (k+=1, k=k+1, k=1+k).

(b)  No.

7. [Extracting the first mark-up tag] (2 points each, 24 points total)

Mark-up languages like HTML and XML use tags encompassed between < and > to provide structural information. For example, the String "

abc=x

" con- tains four tags p, b, /b, and /p in this order.  Suppose we are to write a static method extract that receives a String w and finds the first tag in a given text.  The program should behave as follows: if w either lacks < or lacks > or if in w the first > precedes the first <, extract(w) returns ""; otherwise, the method returns the first tag.

01: public [A] [B] [C]([D] w ) {

02:   int p = w.[E](F); // the position of the first <

03:   int q = w.[G](H); // the position of the first >

04:  if ( p [I]) return "";

05:  if ( q [J]) return "";

06:  if ( p [K] q) return "";

07:   return w.[L];

08: }

Answer

A. static, B. String, C. extract, D. String, E. indexOf,

F.  "<" (or "<",0, '<', '<',0), G. indexOf, H. ">" (or  ">",0,  '>',  '<>,0), I. <0 (or <=-1), J. <0 (or <=-1), K. < (or <=), L. substring(p+1,q)