Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1)Match terminology to definitions. 1.ASCII 2.code path 3.defensive programming

ID: 3544909 • Letter: #

Question





(1)Match terminology to definitions.

1.ASCII


2.code path


3.defensive programming


4.index out of bounds


5.postcondition


6.precondition


7.hrowing an exception


8.Unicode


Answer

A.

must be true before a method starts

B.

an illegal location, e.g., beyond the end of a String

C.

a standard for encoding about 100,000 characters

D.

a possible order for executing statements

E.

a standard for encoding 128 characters

F.

including code to detect mistakes

G.

runtime error

H.

guaranteed to be true when a method ends


Which of the following applies to chars, but not to Strings? (Select all that apply.)


1.

It's a primitive type.


2.

A literal value is surrounded by double quotes.


3.

Its length is exactly one.


4.

You use the equals method for comparing them.


5.

It has methods like startsWith and endsWith.

If c is a char, and s is a String, which of the following expressions will result in a compile error? (Select all that apply.)


1.

s.toUpperCase( )


2.

c.toUpperCase( )


3.

c.equals('A')


4.

s.equals("ABC")


5.

s == "ABC"


6.

Character.toUpperCase(c)


7.

c == 'A'


8.

String.toUpperCase(s)

If c is a char, and s is a String, which of the following expressions will result in a compile error? (Select all that apply.)


1.

s.toUpperCase( )


2.

c.toUpperCase( )


3.

c.equals('A')


4.

s.equals("ABC")


5.

s == "ABC"


6.

Character.toUpperCase(c)


7.

c == 'A'


8.

String.toUpperCase(s)

Answer

A.

'Z'

B.

65

C.

97

D.

'z'

E.

48

F.

'9'

G.

' '

Consider the following code.


String s1 = "2 B or not 2 B";

String s2 = "";

for (int i = 0; i < s1.length( ); i++) {

char ch = s1.charAt(i);

if (true) {

s2 = s2 + ch;

}

}

System.out.println(s2);


Suppose true is replaced with another test. For each replacement test, what will be printed?



Character.isDigit(ch)

Read Answer Items for Question 5


! Character.isDigit(ch)

Read Answer Items for Question 5


Character.isLetter(ch)

Read Answer Items for Question 5


! Character.isLetter(ch)

Read Answer Items for Question 5


Character.isLowerCase(ch)

Read Answer Items for Question 5


Character.isUpperCase(ch)


Answer

A.

BB

B.

B or not B

C.

22

D.

BornotB

E.

2 2

F.

ornot

Which statement replacing the comment will result in printing: B 2 ton ro B 2?


String s1 = "2 B or not 2 B";

String s2 = "";

for (int i = 0; i < s1.length(); i++) {

char ch = s1.charAt(i);

// replace with statement

}

System.out.println(s2);



1.

s1 = s1 + ch;


2.

s1 = ch + s1;


3.

s2 = s2 + ch;


4.

s2 = ch + s2;



Assuming true is replaced with an appropriate test, the geometric mean of two positive numbers is computed by:


public static double geoMean(double a, double b) {

if (! true) {

throw new IllegalArgumentException( );

}


double result = Math.sqrt(a * b);

return result;

}


The geometric mean is not defined if the numbers are not positive. What test should replace true in the method?



1.

(a > 0 || b > 0)


2.

(a > 0 && b > 0)


3.

(a >= 0 && b >= 0)


4.

(a >= 0 || b >= 0)

Assuming true is replaced with an appropriate test, the geometric mean of two positive numbers is computed by:


public static double geoMean(double a, double b) {

if (! true) {

throw new IllegalArgumentException( );

}

double result = Math.sqrt(a * b);

return result;

}


The geometric mean is not defined if the numbers are not positive. Ignoring roundoff error, what is the postcondition of this method?



1.

(result == a || result == b)


2.

(result * result == a * b)


3.

(result + result == a + b)


4.

(result > a && result < b)

Assuming true is replaced with an appropriate test, the following method returns a String consisting of the first and last characters of the String parameter.


public static String firstAndLast(String s) {

if (! true) {

throw new IllegalArgumentException( );

}

int len = s.length( );

String result = s.substring(0,1) + s.substring(len - 1, len);

return result;

}


To avoid an index out of bounds error, what test should replace true in the method?



1.

(s.length( ) < 0)


2.

(s.length( ) > 0)


3.

(s.length( ) < 1)


4.

(s.length( ) > 1)

Assuming true is replaced with an appropriate test, the following method returns a String consisting of the first and last characters of the String parameter.


public static String firstAndLast(String s) {

if (! true) {

throw new IllegalArgumentException( );

}

int len = s.length( );

String result = s.substring(0,1) + s.substring(len - 1, len);

return result;

}


What are the postconditions of this method? (Select all that apply.)



1.

(result.length( ) < s.length( ))


2.

(result.length( ) == 2)


3.

(s.startsWith(result.substring(0,1)))


4.

(s.endsWith(result.substring(1,2)))


5.

(s.equals(result))



(1)Match terminology to definitions.

1.ASCII


2.code path


3.defensive programming


4.index out of bounds


5.postcondition


6.precondition


7.hrowing an exception


8.Unicode


Answer

A.

must be true before a method starts

B.

an illegal location, e.g., beyond the end of a String

C.

a standard for encoding about 100,000 characters

D.

a possible order for executing statements

E.

a standard for encoding 128 characters

F.

including code to detect mistakes

G.

runtime error

H.

guaranteed to be true when a method ends



(2)


Which of the following applies to chars, but not to Strings? (Select all that apply.)


1.

It's a primitive type.


2.

A literal value is surrounded by double quotes.


3.

Its length is exactly one.


4.

You use the equals method for comparing them.


5.

It has methods like startsWith and endsWith.


(3)

If c is a char, and s is a String, which of the following expressions will result in a compile error? (Select all that apply.)


1.

s.toUpperCase( )


2.

c.toUpperCase( )


3.

c.equals('A')


4.

s.equals("ABC")


5.

s == "ABC"


6.

Character.toUpperCase(c)


7.

c == 'A'


8.

String.toUpperCase(s)


(4)

If c is a char, and s is a String, which of the following expressions will result in a compile error? (Select all that apply.)


1.

s.toUpperCase( )


2.

c.toUpperCase( )


3.

c.equals('A')


4.

s.equals("ABC")


5.

s == "ABC"


6.

Character.toUpperCase(c)


7.

c == 'A'


8.

String.toUpperCase(s)

Answer

A.

'Z'

B.

65

C.

97

D.

'z'

E.

48

F.

'9'

G.

' '


(5)

Consider the following code.


String s1 = "2 B or not 2 B";

String s2 = "";

for (int i = 0; i < s1.length( ); i++) {

char ch = s1.charAt(i);

if (true) {

s2 = s2 + ch;

}

}

System.out.println(s2);


Suppose true is replaced with another test. For each replacement test, what will be printed?



Character.isDigit(ch)

Read Answer Items for Question 5


! Character.isDigit(ch)

Read Answer Items for Question 5


Character.isLetter(ch)

Read Answer Items for Question 5


! Character.isLetter(ch)

Read Answer Items for Question 5


Character.isLowerCase(ch)

Read Answer Items for Question 5


Character.isUpperCase(ch)



Answer

A.

BB

B.

B or not B

C.

22

D.

BornotB

E.

2 2

F.

ornot



(6)

Which statement replacing the comment will result in printing: B 2 ton ro B 2?


String s1 = "2 B or not 2 B";

String s2 = "";

for (int i = 0; i < s1.length(); i++) {

char ch = s1.charAt(i);

// replace with statement

}

System.out.println(s2);



1.

s1 = s1 + ch;


2.

s1 = ch + s1;


3.

s2 = s2 + ch;


4.

s2 = ch + s2;





(7)

Assuming true is replaced with an appropriate test, the geometric mean of two positive numbers is computed by:


public static double geoMean(double a, double b) {

if (! true) {

throw new IllegalArgumentException( );

}


double result = Math.sqrt(a * b);

return result;

}


The geometric mean is not defined if the numbers are not positive. What test should replace true in the method?



1.

(a > 0 || b > 0)


2.

(a > 0 && b > 0)


3.

(a >= 0 && b >= 0)


4.

(a >= 0 || b >= 0)


(8)

Assuming true is replaced with an appropriate test, the geometric mean of two positive numbers is computed by:


public static double geoMean(double a, double b) {

if (! true) {

throw new IllegalArgumentException( );

}

double result = Math.sqrt(a * b);

return result;

}


The geometric mean is not defined if the numbers are not positive. Ignoring roundoff error, what is the postcondition of this method?



1.

(result == a || result == b)


2.

(result * result == a * b)


3.

(result + result == a + b)


4.

(result > a && result < b)


(9)

Assuming true is replaced with an appropriate test, the following method returns a String consisting of the first and last characters of the String parameter.


public static String firstAndLast(String s) {

if (! true) {

throw new IllegalArgumentException( );

}

int len = s.length( );

String result = s.substring(0,1) + s.substring(len - 1, len);

return result;

}


To avoid an index out of bounds error, what test should replace true in the method?



1.

(s.length( ) < 0)


2.

(s.length( ) > 0)


3.

(s.length( ) < 1)


4.

(s.length( ) > 1)


(10)

Assuming true is replaced with an appropriate test, the following method returns a String consisting of the first and last characters of the String parameter.


public static String firstAndLast(String s) {

if (! true) {

throw new IllegalArgumentException( );

}

int len = s.length( );

String result = s.substring(0,1) + s.substring(len - 1, len);

return result;

}


What are the postconditions of this method? (Select all that apply.)



1.

(result.length( ) < s.length( ))


2.

(result.length( ) == 2)


3.

(s.startsWith(result.substring(0,1)))


4.

(s.endsWith(result.substring(1,2)))


5.

(s.equals(result))


Explanation / Answer

1)

  

1.--->E

2.--->D

3.--->H

4.--->B

5.--->F

6.--->A

7.--->G

8.--->C


2)

applied to char and not to string--1,3,4


3)


ans--2,3,4,5


4)