Practice

advertisement
1. Mark the following statements as true or false.
a. An identifier can be any sequence of digits and letters.
b. In Java, there is no difference between a reserved word and a predefined identifier.
c. A Java identifier can start with a digit.
d. The operands of the modulus operator must be integers.
e. If the value of a is 4 and the value of b is 3, then after the statement a = b; executes, the value
of b is still 3.
f. In an output statement, the newline character can be a part of the string.
g. The following is a legal Java program:
public class JavaProgram
{
public static void main(String[] args)
{
}
}
h. In a mixed expression, all the operands are converted to floating-point numbers
i. Suppose x = 5.After the statement y = x++; executes, y is 5 and x is 6
j. Suppose a = 5.After the statement ++a; executes, the value of a is still 5 the value of the
expression is not saved in another variable.
2. Which of the following are valid Java identifiers?
a. RS6S6
b. MIX-UP
c. STOP!
d. examl
C. SeptemberlLecture
f. 2May
g. Mike’s
h. First Exam
i. J
j. Three
3. Which of the following is a reserved word in Java?
a. mt
b. INT
c. Char
d. CHAR
4. Choose the best answer.
a. The value of 15 / 2 is:
(i) 7 (ii) 7.5
(iii) 7 1/2
b. The value of 18 / 3 is:
(i) 6 (ii) 0.167
(iii) 6.0
c. The value of 22 % 7 is:
(i) 3 (ii) 1
(iii) 3.142
d. The value of 5 % 7 is:
(i) 0
(ii) 2
(iii) 5
(iv) 0.75
(v) none of these
(iv) none of these
(iv) 22/7
(iv) undefined
e. The value of 17.0 % 4 is:
(i) 4
(ii) 4.25
(iii) 4%
f. The value of 5 - 3.0 + 2 is:
(i) 0
(ii) 0.0
(iii) 4
g. The value of 7 - 5 * 2 + is:
(i) -2 (ii) 5
(iii) 6
h. The value of 15.0 / 3.0 + 2.0 is:
(i) 3 (ii) 3.0
(iii) 5
(iv) undefined
(iv) 4.0
(iv) none of these
(iv) none of these
5. If x = 5, y = 6, z = 4, and w = 3.5, evaluate each of the following expressions,
if possible. If it is not possible, state the reason.
a. (x + z) % y
b. (x + y) % w
c (y + w) % x
d. (x + y ) * w
e. (x % y) % z
f. (y % z) % x
g. (x * 2) % y
h. ((x * y) * w) * 2
6. Given:
int n, m, l;
double x, y;
which of the following assignments are valid? If an assignment is not valid, state the reason.
When not noted, assume that each variable is declared.
a. n = m = 5;
b. m = l = 2 * n;
c. n= 5; m 2 + 6; n = 6 / 3;
d. m + n = l;
e. x = 2 * n + 5.3;
f. 1 + 1 =
g. x / y = x * y;
h. m = n % 1;
i. n = x % 5;
j. x = x + 5;
k. n = 3 + 4.6
7. Do a walk-through to find the value assigned to e. Assume that all variables are properly
declared.
a = 3;
b = 4;
c = (a % b) * 6;
d = c / b;
e = (a +b + c + d)/ 4;
8. Which of the following variable declarations are correct? If a variable declaration is not
correct, give the reason(s) and provide the correct variable declaration.
n = 12;
//Line 1
char letter = ;
//Line 2
int one = 5, two;
//Line 3
double x, y, z; //Line 4
9. Which of the following are valid Java assignment statements? Assume that i, x, and percent
are double variables.
a. i = i + 5;
b. x + 2 = x;
c. x = 2.5 * x;
d. percent = 10%
10. Write Java statements that accomplish the following.
a. Declare the int variables x and y.
b. Initialize an int variable x to 10 and a char variable ch to ‘B’.
c. Update the value of an int variable x by adding 5 to it.
d. Set the value of a double variable z to 25.3.
e. Copy the content of an int variable y into an int variable z.
£ Swap the contents of the int variables x and y. (Declare additional variables, if necessary.)
g. Output the content of a variable x and an expression 2 * x + 5 - y, where x and y are double
variables.
h. Declare a char variable grade and sets the value of grade to ‘A’.
i. Declare int variables to store four integers.
j. Copy the value of a double variable z to the nearest integer into an int variable x.
11. Write each of the following as a Java expression.
a. -10 times a
b. The character that represents 8
c. (b2— 4ac)/ 2a
d. (-b ÷ (b2— 4ac)) / 2a
12. Suppose x, y, z, and w are int variables. What value is assigned to each variable after the last
statement executes?
x = 5; z = 3;
y = x - z;
z = 2 * y + 3;
w = x - 2 * y + z;
z = w - x;
w++;
13. Suppose x, y, and z are int variables and w and t are double variables. What value is assigned
to each variable after the last statement executes?
x = 17;
y = 15;
x = x + y / 4;
z = x % 3 + 4;
w = 17 / 3 + 6.5;
t = x / 4.0 + 15 % 4 - 3.5;
14. Suppose x and y are int variables and x = 25 and y = 35.What is the output of each of the
following statements?
a. System.out.println (x + ' ' + y);
b. System.out.println (x + " " + y);
15. Suppose x, y, and z are int variables and x = 2, y = 5,and z = 6.What is the
output of each of the following statements?
a. System.out.println("x = " + x + ", y = + y + ", z = " + z);
b. Systen.out.println("x + y = " + (x + y));
c. System.out.println("Sum of " + x + " and " + z + " is + (x + 2));
d. System.out.println("z / x = " + (z / x));
e. System.out.println("2 times " + x + " = " + (2 * x));
16. What is the output of the following statements? Suppose a and b are int variables, c is
a double variable, and a = 13, b = 5, and c = 17.5.
a. System.out.println(a + b- c);
b. System.out.println(15 / 2 + c);
c. System.out.println(a / (double)(b) + 2 * c);
d. System.out.println(14 % 3 + 6.3 + b / a);
e. System.out.println((int)(c)% 5 + a — b);
f. System.out.println(13.5 / 2 + 4.0 * 3.5 + 18);
17.How do you print the carriage return?
18. Which of the following are correct Java statements?
a. System.out.println("Hello There!");
b. System.out.println ("Hello");
(" There!");
c. System.out.println("Hello"” +
" There!");
d. System.out.println( 'Hello There!');
19. The following two programs have syntax mistakes. Correct them. On each
successive line, assume that any preceding error has been corrected.
public class ProgWithErrorsA
{
static final int PRIME = 11,213;
static final RATE 15.6
public void main(String[] arg)
{
int i, x, y w;
x = 7;
y = 3;
x = x + w;
PRIME = x + PRIME;
System. out.println (PRIME);
wages = rate * 36.75;
System.out.println("Wages = " wages);
}
}
b.
public class ProgWithErrorsB
{
static final char BLANK = ' ';
static final int ONE 5;
public static void main(String[] arg)
{
int a, b, cc;
a = one + 5;
b = a + BLANK;
cc := a + ONE * 2;
a + cc = b;
ONE = b + c;
System.out.println("a = " + a
+ ", b = " + b
+ ", cc = " + cc);
}
}
20. Write equivalent compound statements if possible.
a. x = 2 * x
b. x = x + y - 2;
c. sum = sum + nun;
d. 2 = 2 * x + 2 *
e. y = y / (x + 5);
21. Write the following compound statements as equivalent simple statements.
a. x += 5 – z;
b. y = 2 * x + 5 – z;
c. w += 2 * z + 4;
d. x -= z + y – t;
e. sum += num;
f. x/= y - 2;
22, Suppose a, b, and c are int variables and a = 5 and b = 6.What value is assigned to each
variable after each statement executes? If a variable is undefined at a particular statement, report
UND (undefined).
a
b
c
a = (b++) + 3;
c = 2 * a + (++b);
b = 2 * (++c) — (a++);
23. Suppose a,b, and sum are int variables and c is a double variable. What value is assigned to
each variable after each statement executes? Suppose a = 3, b = 5, and c = 14.1.
a
b
c
sum
sum = a + b + (int)c;
c /= a;
b += (int)c — a;
a = 2 * b + (int)c;
24. What is printed by the following program? Suppose the input is:
20 15
import java.util.*;
public class Mystery
{
static Scanner console = new Scanner(System.in);
static final int NUM = 10;
static final double X = 20.5;
public static void main(String[] arg)
{
int a, b;
double z;
char grade;
a = 25;
System.out.println("a = " + a);
System.out.print("Enter the first integers: ");
System. out . f lush( );
a = console.nextInt ();
System.out.println();
System.out.print ("Enter the second integers: ");
System.out.f lush();
b = conso1e.nextInt();
System.out.println ();
System.out.println(”The numbers you entered are "
+ a + " and " + b);
z = x + 2 * a - b;
System.out.println("z = " + z);
grade = 'A';
System.out.println ("Your grade is " + grade);
a = 2 * NUM + (int) z;
System.out.println("The value of a = " + a);
}
}
25. What type of input does the following program require, and in what order must the input be
provided?
import java.util.*;
public class Strange
{
static Scanner console = new Scanner(System.in);
public static void main(String[] arg)
{
int x, y;
String name;
x = console.nextlnt();
name = console,nextLine();
y = console.nextlnt();
}
}
Reference: Java Programming: Program Design Including Data Structures by D.S. Malik
Download