CSIT114: Namita Singla Sample Exam1 Page 1 of 5

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?

  1. boolean a = (2.5 == (double)(5 / 2));
  1. boolean a = (3 == (1 + 2 * 11 % 4));
  1. double a = 10/3;
  2. double a = (double)10/3;
  1. 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

}
}

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)

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?

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?