Programming and Problem Solving with Java

(やまだぃちぅ) #1

146


10.Given the statements
String heading;
String str;

heading = “Exam Preparation Exercises”;
what is the output of each of the following code segments?
a.System.out.println(heading.length());
b.System.out.println(heading.substring( 6 , 16 ));
c. System.out.println(heading.indexOf(“Ex”));
d.str = heading.substring( 2 , 26 );
System.out.println(str.indexOf(“Ex”));
e.str = heading.substring(heading.indexOf(“Ex”) + 2 , 24 );
System.out.println(str.indexOf(“Ex”));
f. str = heading.substring(heading.indexOf(“Ex”) + 2 , heading.length());
System.out.println(str.indexOf(“Ex”));
11.Incorrectly formatting an application causes an error. (True or False?)

Programming Warm-Up Exercises


1.Change the application in Exam Preparation Exercise 6 so that it prints the cost
for 15 pounds.
2.Write an assignment statement to calculate the sum of the numbers from 1
through nusing Gauss’s formula:

sum=

Store the result into the intvariable sum.
3.Given the declarations
int i;
int j;
doublex;
doubley;
write a valid Java expression for each of the following algebraic expressions.

a. – 3 e. (the floating-point result)

b.(x+ y)(x– y) f. ij(the integer quotient)

i
j

x
y

n(n+ 1)
2
Free download pdf