Digital MP4 = new Digital();
(b) State the values stored in the variables str1 and str2:
String s1 = "good";
String s2 = "world matters";
String str1 = s2.substring(5).replace('t', 'n');
String str2 = s1.concat(str1);
Value stored in str1 is " manners" and str2 is "good manners". (Note that str1 begins with a space.)
s2.substring(5) gives a substring from index 5 till the end of the string which is " matters". The replace method replaces all 't' in " matters" with 'n' giving " manners". s1.concat(str1) joins together "good" and " manners" so "good manners" is stored in str2.
(c) What does a class encapsulate?
A class encapsulates data and behavior.
(d) Rewrite the following program segment using the if..else statement:
comm = (sale > 15000)? sale * 5 / 100 : 0;
if(sale > 15000)
comm = sale * 5 / 100;
else
comm = 0;
(e) How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do{
++x;
y -= x++;
}while(x <= 10);
return y;
The loop will run 5 times and the value returned is 15.
(f) What is the data type that the following library functions return?
- isWhitespace(char ch)
- Math.random()
- boolean
- double
(g) Write a Java expression for:
$ut + \frac{1}{2}ft^2$
u * t + 1 / 2.0 * f * t * t
(h) If int n[] = {1, 2, 3, 5, 7, 9, 13, 16}, what are the values of x and y?
x = Math.pow(n[4], n[2]);
y = Math.sqrt(n[5] + n[7]);
x = Math.pow(n[4], n[2]);
⇒ x = Math.pow(7, 3);
⇒ x = 343.0;
y = Math.sqrt(n[5] + n[7]);
⇒ y = Math.sqrt(9 + 16);
⇒ y = Math.sqrt(25);
⇒ y = 5.0;
(i) What is the final value of ctr when the iteration process given below, executes?
int ctr = 0;
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 5; j += 2)
++ctr;
The final value of ctr is 15.
The outer loop executes 5 times. For each iteration of outer loop, the inner loop executes 3 times. So the statement ++ctr; is executed a total of 5 x 3 = 15 times.
(j) Name the method of Scanner class that:
- is used to input an integer data from the standard input stream.
- is used to input a String data from the standard input stream.
- nextInt()
- nextLine()