Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Friday, October 29, 2010
Eksekutif.Java 2
1. What is the answer for Math.sqrt(-9)?
a. -3 b. 3 c. 81 d. NaN
2. What is the output of the following statement?
int iNum = 12;
double dNum = iNum/10;
System.out.println(iNum + " " + dNum);
a. 12 1.2 b. 12.0 1.0 c. 12 1.0 d. 12.0 1.2
3. What is the output of the following statement?
int iNum = 12;
double dNum = (double) iNum/10;
System.out.println(iNum + " " + dNum);
a. 12 1.2 b. 12.0 1.0 c. 12 1.0 d. 12.0 1.2
4. Which of the following is NOT a relational (comparison) operator in Java?
a. = b. != c. >= d. <=
5. The result of the boolean expression 3 + 5 <= 8 is
a. true b. false c. 0 d. The expression will generate a compile error
6. The result of the boolean expression 'B' < 'a' is
a. true b. false c. 0 d. The expression will generate a compile error
7. The result of the boolean expression '3' < 'C' is
a. true b. false c. 0 d. The expression will generate a compile error
8. The result of the boolean expression 3 < '3' is
a. true b. false c. 0 d. The expression will generate a compile error
9. Which of the following contains only relational operators?
a. = < > b. <= >= ==
c. =< >= == d. < <> >
10. Consider the following code fragment:
sc refers to an instance of Scanner class, which is created as:
Scanner sc = new Scanner (System.in);
int x = sc.nextInt();
if (x >= 0)
System.out.println("The input is ") ;
System.out.println(x);
Which of the following statements is true?
The value of x always gets printed out.
The value of x gets printed out only when the input is positive or zero.
The value of x gets printed out only when the input is negative.
The value of x never gets printed out.
11. What will be the value of num after the following section of code executes:
int num = 15;
if (num > 10)
num = num * 2;
else
num = num - 2;
a. 8 b. 13 c. 20 d. 30
12. The output for the following fragment code is
int num = 11;
if (num >= 10)
num = num / 2;
System.out.println(num);
System.out.println("Done");
a. Done b. 5 c. 5.5 d. 11
Done Done Done
13. What will the following code print?
int code = 321 % 4;
if (code == 0)
System.out.println("KL");
else if (code == 1)
System.out.println("PJ");
else if (code == 2)
System.out.println("SA");
else
System.out.println("Klang");
}
a. KL b. PJ c. SA d. Klang KL
14. Consider the following code fragment:
sc refers to an instance of Scanner class, which is created as:
Scanner sc = new Scanner (System.in);
int x;
x = sc.nextInt() ;
if (x >= 0)
System.out.println("positive input:");
System.out.println(x);
else
System.out.println("negative input:");
System.out.println(x);
Which of the following statements is true?
The code fragment will not compile.
If the value entered for x is zero no output occurs.
If the value entered for x is positive, "positive input:" is displayed, followed by the value of x, displayed once.
If the value entered for x is positive, "positive input:" is displayed, followed by the value of x, displayed twice.
Friday, October 8, 2010
Eksekutif.Java
1. What are the TWO different data types available in Java language?
19. Which of the following Java expressions correctly represents the algebraic expression: ?
25. The difference between the types int and byte is
32. Fill in the arithmetic operator (either +, -, /, * or %) so that both sides of the equations evaluate to the same answer. List TWO possibilities for each equation, as shown in the example: Solution: (a) 7 - 6 -1 = 8 % 4 or 7 % 6 - 1 = 8 % 4
- Primitive data type and built-in data type
- Primitive data type and reference data type
- Class type and reference data type
- Enumerated data type and reference data type
2. How many different types are there in primitive data type?
a. 1 b. 3 c. 6 d. 8
3. Which of the following is NOT of integer type?
a. byte b. double c. int d. long
4. Which of the following is NOT a valid integer literal?
a. 1,500 b. 888 c. +123 d. -456
5. Which of the following is NOT a valid integer literal?
a. 12 b. -34.5 c. +678L d. -90
6. Which of the following is NOT a valid floating-point literal?
a. -98.7 b. +6.5 c. +4.3F d. -2,001.0
7. The _____ data type stores integer values in 8-bit signed locations from -128 to +127.
a. long b. char c. byte d. short
8. Which of the following is NOT a valid Java identifier?
a. formula1 b. java c. 2ndPrize d. _NotCommon
9. Which of the following is NOT a reserved word in Java?
a. int b. char c. boolean d. Struct
10. Which of the following is a reserved word in Java?
a. while b. Public c. Repeat d. Int
11. How many standard arithmeticoperators are there in the Java programming language?
a. four b. five c. eleven d. fourteen
12. Which of the following elements is NOT required in a variable declaration?
a. A type b. An identifier c. An assigned value d. A semicolon
13. The location of a Java variable refers to
a. a place in memory b. a line number in a program
c. a file name d. none of the above
14. The symbol = in the Java programming language represents the
a. equal operator b. assignment operator
c. subtraction operator d. none of the above
15. To declare a variable with the name quantity for storing values of type 'int', we use the statement
a. quantity int; b. int Quantity;
c. integer quantity; d. int quantity;
16. To declare an 'int' variable named num with an initial value 99, you write
a. int num = "99"; b. int num = 99;
c. 99 = num int; d. int num = ninety nine;
17. The data type for the variable number shown below is most likely to be _____.
_____ number = 12.5;
a. int b. float c. double d. none of the above
18. Which of the following assignment statements is NOT legal assuming num1 and num2 are declared as 'int'?
a. num1 = num2; b. num1 = num2 + 10;
c. num1 = num1 + num2; d. num1 + 10 = num2;
19. Which of the following Java expressions correctly represents the algebraic expression: ?
20. Consider the following fragament of code:
int x = 5;
int y =10;
y = x;
What are the values of x and y?
a. x is 5 and y is 10 b. x is 10 and y is 5 c. x is 5 and y is 5 d. x is 10 and y is 10
21. What is the answer for 9 / 5?
a. 1 b. 1.0 c. 1.8 d. 4
22. What is the answer for 14 % 4?
a. 2 b. 2.0 c. 3.5 d. 10
23. The diagram on the right shows how the integer literal 5 assigned to the variable named num is being stored in the computer’s main memory. What is the most appropriate data type for num?
24. The diagram on the right shows how the integer literal 5 assigned to the variable named num is being stored in the computer’s main memory. What is the most appropriate data type for num?
25. The difference between the types int and byte is
a. int can hold both integer and floating point numbers
b. int can hold larger integer numbers than byte
c. byte can hold smaller integer numbers than int
d. int can hold only floating numbers
26. The literal value 1.5f
- is a single-precision floating-point number (float)
- is a double-precision floating-point number (double)
- is a String value
- is not a valid literal value of any type in Java
27. In Java the variables price and PRICE
a. are the same b. may be the same or different depending on what computer you use
c. are different d. are not allowed as variable names
28. Which of the following assignment statements is illegal?
a. byte one = -34; b. long two = 56; c. short three = 1; d. int ten = 10.0;
29. What is the value of the following expression: 5 + 6 / 4 - 3?
a. -0.25 b. 3 c. 3.5 d. 11
30. What is the value of the following expression: (5 + 6)/4 - 3?
a. -1 b. -0.25 c. 3 d. 11
31. Write Java statements for the following:
a. p = 2(q + r) b. x = y2 + z2 c. p = m2 + (n - 1)2
32. Fill in the arithmetic operator (either +, -, /, * or %) so that both sides of the equations evaluate to the same answer. List TWO possibilities for each equation, as shown in the example:
7 6 1 = 8 4
Solution: (a) 7 - 6 -1 = 8 % 4 or 7 % 6 - 1 = 8 % 4
(b) 7 / 6 +1= 8 / 4 or 7 - 6 + 1 = 8 / 4
NOTE: In Java, integer division always results in an integer answer. The decimal part is truncated.
a) 6 5 = 7 4
b) 5 3 2 = 2 5
c) 11 3 4 = 6 2
d) 9 4 1 = 21 2 12
e) 7 5 1 = 11 2
Subscribe to:
Posts (Atom)