Practice Exam Solution

advertisement
CSIT114: Namita Singla
Sample Exam1
Page 1 of 8
CSIT114 Introduction to Java
Spring 2006
Sample Exam Solutions
Namita Singla
Mar 16, 2006
Name: Solutions
Note: I have given very explanatory answers to explain things. You don’t need to
explain them in exam like this. This exam is just to give you an idea of the type of
questions I might give on the exam.
Question 1:
Given the following local variable declarations:
String s = “abc”;
String a = "Did Hannah see bees? Hannah did.”;
String t;
What is the value of the following expressions (or ERROR)?
1.
3
s.length()
s is pointing to “abc” and “abc” has three characters.
2.
ERROR
t.length()
Since t is not pointing to any object, therefore t.length() will throw Null
pointer error. Note that t is null.
3.
‘a’
a.charAt(5)
a is pointing to “Did Hannah see bees? Hannah did.”. charAt(index)
method returns the character at the specified index. Since the index of first
character in a String is 0 so character at index 5 is ‘a’.
D
0
i
1
d
2
3
H
4
a
5
n
n
a
h
s
--
--
Note that I have put single quotes around a because the result is a character.
4.
”ABC”
s.toUpperCase()
CSIT114: Namita Singla
Sample Exam1
Page 2 of 8
s is pointing to “abc”. toUpperCase() will convert all the lowercase
letters to uppercases. Note that I have put double quotes around ABC because
the result is a String.
5. “abc1”
s+1
Here 1 which is a number literal will be promoted to String and String
concatenation operator will be applied. The result again has double quotes
around it to show the fact that it is String.
6.
a.indexOf(‘H’)
4
a is pointing to “Did Hannah see bees? Hannah did.”. indexOf(char)
method returns the index of the first occurrence of the specified character
D
0
7.
i
1
d
2
a
5
H
4
3
n
n
a
h
s
--
--
"Tomorrow".lastIndexOf(‘o’)
6
Note here that there are three occurrences of ‘o’. lastIndexOf(char) will
return the index of the last occurrence of the specified character.
T
0
8.
“mo”
o
1
m
2
o
3
r
4
r
5
o
6
w
7
"Tomorrow".substring(2,4)
substring(offset, endIndex) returns a new String starting at index offset
and extending through endIndex – 1. So substring(2,4) will return a String
starting at index 2 and extending through (4-1) i.e. 3
T
0
9.
true
o
1
r
4
m o
2 3
r
5
o
6
w
7
s.substring(1,3).equals("bc")
“bc”
s is pointing to “abc” so s.substring(1,3) will return the String starting
from index 1 and extending through (3-1) i.e. 2. The String is “bc”.
a
b
c
0
1
2
CSIT114: Namita Singla
Sample Exam1
Page 3 of 8
equals(String) method returns true if this String contains the same characters
as that of the String as argument and false otherwise. So actually we are doing
“bc”.equals(“bc”) which is true.
10.
false
(s.length() + s).startsWith("a")
“3abc”
(s.length() + s) is equal to “3abc” because length of s is 3 and then we can
apply String concatenation operator to get “3abc”. So we can say that we are
actually doing:
“3abc”.startsWith(“a”) which is false
startsWith(String) tests if this string starts with the specified prefix and
“3abc” does not start with “a”.
Question 2
What is the value of variable a after executing each of the following?
a. boolean a = (2.5 == (double)(5 / 2));
= is an assignment operator which has least precedence. So first of all the
expression on the right (2.5 == (double)(5 / 2)) will be evaluated.
Here we have nested parentheses. So (5/2) will be evaluated first. This is
integer division so the result will be 2. Now because of the casting operator
(double), 2 will be converted to 2.0. So actually we are checking:
(2.5 == 2.0) which is false.
Therefore value of a is false.
b. boolean a = (3 == (1 + 2 * 11 % 4));
= is an assignment operator which has least precedence. So first of all the
expression on the right (3 == (1 + 2 * 11 % 4)) will be evaluated.
Here we have nested parentheses. So (1 + 2 * 11 % 4) will be evaluated
first. This will give 3 and (3 == 3) is true. Therefore a is true.
c. double a = 10/3;
10/3 will give result 3 (integer division truncates). But 3 will be
promoted to 3.0 because a is a variable of type double. Therefore a is 3.0.
CSIT114: Namita Singla
Sample Exam1
Page 4 of 8
d. double a = (double)10/3;
Casting operator has higher precedence than /. 10 will be converted to
10.0. Now 10.0 /3 will give 3.333. Therefore a is 3.333.
e. boolean a = (2.5 == (double)5 / 2);
= is an assignment operator which has least precedence. So first of all the
expression on the right (2.5 == (double)5 / 2) will be evaluated.
Casting operator has higher precedence than /. 5 will be converted to 5.0.
Now 5.0 /2 will give 2.5. So actually we are checking:
(2.5 == 2.5) which is true.
Therefore value of a is true.
Question 3:
Complete the following class called MyClass. This class consists of a single main
method that asks the user to input an integer. The main method tells whether the
number is even or odd.
A sample output for your class would be:
Enter an integer: 12
12 is an even number
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = input.nextInt();
if(num % 2 == 0)
System.out.print(num+" is an even number");
else
System.out.print(num+" is an odd number");
}
}
CSIT114: Namita Singla
Sample Exam1
Page 5 of 8
Question 4:
public class MySwitch {
public static void main(String[] args) {
int k=10;
switch(k) {
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
default:
System.out.println("This is the default output");
break;
}
}
}
(a) What is the name of above class?
.
MySwitch
(b) List all the java reserved words in the above class.
public, class, static, void, int, switch, case, break,
default
(c) What will happen when you attempt to compile the above code? Will you get any
output? If yes then write the output.
The above code will compile properly. Yes there will be output. Output will be
ten
twenty
Note that there is no break statement for case 10. Since k is equal to 10 the
execution will start from case 10. It will print ten. Then it will go to case 20, print
twenty and will finally come out of switch conditional because of break in case
20.
CSIT114: Namita Singla
Sample Exam1
Page 6 of 8
Question 5: Given the following declarations, what is the value of each of the following
expressions? Write ERROR for any error that may occur.
int x = 4;
int y = 0;
int z = 3;
boolean flag = true;
a. ((x > z) || flag)
Because of nested parentheses (x > z) will be evaluated first. (x > z) is
true and flag is also true. So result is true
b. “Value:”+ x + x/2
/ has higher precedence than +. x/2 will be evaluated first. This will give
2. Now since + is left to right associative so “Value:”+ x will be
evaluated first. This will promote 4 to “4” and will give result as
“Value:4”. Now 2 will also be promoted to “2” and final result will be
“Value:42”.
c. “Value:”+ (z + z/2)
Because of parentheses (z + z/2) will be evaluated first. This will give
4. Now 4 will be promoted to “4” and final result will be “Value:4”.
d. (y == 0) || (2/y == 0)
(y == 0) is true and since || is short circuited so there is no need to
evaluate (2/y == 0) because no matter what the result of (2/y == 0)
will be the final result is always true. Answer is true
e. !flag || (2/y == 0)
!flag is false. Therefore (2/y == 0) must be evaluated to evaluate
the whole expression. (2/y == 0) will give “Divide by zero” error.
Answer is ERROR
Question 6:
a. Give an example of a relational operator. <, <=, >, >=
b. Give an example of a primitive date type. int, short, long, byte, float, double,
char, boolean
c. Give an example of a logical operator. &&, ||, !
CSIT114: Namita Singla
Sample Exam1
Page 7 of 8
d. Give an example of Conditional statement. if statement, if-else statement,
switch statement.
e. Name any class from Java class library. String, System, Random, Math,
Scanner, Graphics, JApplet
f. Which java reserved word is used to declare constant variables? final
g. What is a compiler?
Program that translates code in one language to equivalent code in another
language.
h. Every compiled program in java must have a main method? TRUE/ FALSE
FALSE. Although every application (executable program) in java must have
a main method.
i. Why java is called a fully object oriented language?
Because you can not write non object oriented programs in Java.
Question 7:
Consider the following code fragment:
int sum = 0;
int i = 0;
while (i < 5)
{
sum += i;
i++;
}
System.out.print(sum);
Replace the while loop in the fragment above with a for loop that prints the same value
of sum variable.
for(int i = 0; i < 5; i++)
{
sum += i;
}
CSIT114: Namita Singla
Sample Exam1
Page 8 of 8
Question 8:
Consider the following loop:
Initialization
Test
Update
for (int count = 0; count < 7; count++ )
{
System.out.println(2*count+1);
Body
}
Circle and label the 4 parts of a loop (initialization, loop test, loop update, loop body).
What is printed to the console during execution of the above code?
1
3
5
7
9
11
13
Download