CSIT114: Namita Singla Sample Exam1 Page 1 of 5 CSIT114 Introduction to Java

advertisement
CSIT114: Namita Singla
Sample Exam1
CSIT114 Introduction to Java
Spring 2006
Sample Exam 1
Namita Singla
Mar 16, 2006
Name:____________________________________________________
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. __________ s.length()
2. __________ t.length()
3. __________ a.charAt(5)
4. __________ s.toUpperCase()
5. __________ s + 1
6. __________ a.indexOf(‘H’)
7. __________"Tomorrow".lastIndexOf(‘o’)
8. __________"Tomorrow".substring(2,4)
9. __________ s.substring(1,3).equals("bc")
10. __________ (s.length() + s).startsWith("a")
Question 2
What is the value of variable a after executing each of the following?
a. boolean a = (2.5 == (double)(5 / 2));
b. boolean a = (3 == (1 + 2 * 11 % 4));
c. double a = 10/3;
Page 1 of 5
CSIT114: Namita Singla
Sample Exam1
Page 2 of 5
d. double a = (double)10/3;
e. boolean a = (2.5 == (double)5 / 2);
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();
// your code goes here
}
}
CSIT114: Namita Singla
Sample Exam1
Page 3 of 5
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?
.
(b) List all the java reserved words in the above class.
(c) What will happen when you attempt to compile the above code? Will you get any
output? If yes then write the output.
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)
CSIT114: Namita Singla
Sample Exam1
Page 4 of 5
b. “Value:”+ x + x/2
c. “Value:”+ (z + z/2)
d. (y == 0) || (2/y == 0)
e. !flag || (2/y == 0)
Question 6:
a. Give an example of a relational operator. ______________
b. Give an example of a primitive date type. ___________________
c. Give an example of a logical operator.___________________
d. Give an example of Conditional statement. ______________
e. Name any class from Java class library.___________________
f. Which java reserved word is used to declare constant variables? ___________
g. What is a compiler?
h. Every compiled program in java must have a main method? TRUE/ FALSE
i. Why java is called a fully object oriented language?
CSIT114: Namita Singla
Sample Exam1
Page 5 of 5
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.
Question 8:
Consider the following loop:
for (int count = 0; count < 7; count++)
{
System.out.println(2*count+1);
}
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?
Download