Exam 01-Theory
Name: ______Yasin Patel
Score:
True and False (T / F) (1’ x 6)
1.
2.
3.
4.
5.
6.
( T ) 11 / 2 + 3.5 equals to 9.0
( F ) 0b11011 % 0h3 equals 1
( F ) A float variable takes twice as much of space as int variable
( F ) 0o3 is bigger than 0h3
( F ) (int) 3.7 + 4.5 equals 8
( T ) char character = 65; is a legal statement
Multiple-choice (A, B, C or D) (1’ X 06)
1. ( c )
a.
b.
c.
d.
String str1 = “I love Java!”;
String str2 = str1.substring(str1.indexOf(‘ ’) + 1, str1.length() - 1);
Then str2.length() equals _____
7
8
9
10
2. ( d ) double num = 7.14;
num -= 3.14 - 2 % 4;
num equals ____
a. 0.0
b. 2.0
c. 4.0
d. 6.0
3. ( b ) A char variable takes the same size in the memory as _____ variable
a. byte
b. short
c. int
d. long
4. ( c ) Assume you have
String course = “Java”;
int score = 82;
If you want to print something like:
Course---:-Java
Score----:---82
where - represents space, we can use:
a.
b.
c.
d.
String.format(“%9s:%-5s\n%9s:%-5d\n”, course, score);
String.format(“%3s:%-1s\n%4s:%-3d\n”, course, score);
String.format(“%-9s:%5s\n%-9s:%5d\n”, course, score);
String.format(“%-3s:%1s\n%-4s:%3d\n”, course, score);
5. ( c ) (double) (int) 8.7 + (char) (int) (double) 8.7 equals ______
a.
b.
c.
d.
17.4
16.7
16.0
16
6. ( b ) If you have String str = “3.14159265354” + 1;
Then “” + str.charAt(0) + str.charAt(str.length() - 1) equals ____
a.
b.
c.
d.
“44”
“31”
“41”
“34”
Estimate the result of the following code show your calculation steps: (If
Error, write “Error”, and explain why) (1’ X 10)
Assume Scanner console = new Scanner(System.in); is declared.
1. __10011101__________
Convert (D)314 to (B)
2. ____2________
System.out.print(2 * 10 % 3 + 25 / 4 % 1);
3. ____6.0________
System.out.print(Math.ceil(Math.floor(Math.pow(16, 0.5)) + 2 / Math.pow(1, 1)));
4. ______W______
char chara = console.nextLine().charAt(0);
System.out.print(chara);
// user input “Wait, what?”
5. ___b_________
char chara = console.nextLine().charAt(0);
++chara;
System.out.print(chara++);
// user input “abc”, ASCII of ‘a’ is 97
6. String str = ________________________________;
Filling the missing part to create a string: The current path is “C:\User\Java”
7. ______4______
int num1 = console.nextInt() / 4;
int num2 = console.nextInt() / 4;
// user input 9
// user input 2
System.out.println(“num1 + num2 = ” + num1 + num2);
8. ___8
int int1 = 2;
char char1 = ‘6’ – ‘0’;
System.out.println(char1 + int1);
9. _____d_______
int num = -2;
char character = ‘c’;
character -= ++num;
System.out.println(character);
10. ____WINTER is coming.________
String str = “Winter is coming.”;’
System.out.println(str.substring(0, str.indexOf(‘ ‘) + 1).toUpperCase()
+ str.substring(str.indexOf(‘ ‘)).toLowerCase());