CSIT114: Namita SinglaSample Exam1Page 1 of 8

CSIT114 Introduction to Java

Spring 2006

Sample Exam Solutions

Namita Singla

Mar16, 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.

  1. ERROR t.length()

Since t is not pointing to any object, therefore t.length() will throw Null pointer error. Note that t is null.

  1. ‘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 / i / d / H / a / n / n / a / h / s / -- / --

0 1 2 3 4 5

Note that I have put single quotes around abecause the result is a character.

  1. ”ABC” s.toUpperCase()

s is pointing to “abc”. toUpperCase() will convert all the lowercase letters to uppercases. Note that I have put double quotes around ABCbecause the result is a String.

  1. “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.

  1. 4 a.indexOf(‘H’)

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 / i / d / H / a / n / n / a / h / s / -- / --

0 1 2 3 4 5

  1. 6 "Tomorrow".lastIndexOf(‘o’)

Note here that there are three occurrences of‘o’. lastIndexOf(char) will return the index of the last occurrence of the specified character.

T / o / m / o / r / r / o / w

0 1 2 3 4 5 6 7

  1. “mo” "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 / o / m / o / r / r / o / w

0 1 2 3 4 5 6 7

  1. true s.substring(1,3).equals("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

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.

  1. false (s.length() + s).startsWith("a")

(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?

  1. 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 ais false.

  1. 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.

  1. 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.

  1. 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.

  1. 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 ais 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");

}
}

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

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 twentyand will finally come out of switch conditional because of break in case 20.

Question 5: Given the following declarations, what is the value of each of the following expressions? Write ERRORfor any error that may occur.

int x = 4;

int y = 0;

int z = 3;

boolean flag = true;

  1. ((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

  1. “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”.

  1. “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”.

  1. (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

  1. !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:

  1. Give an example of a relational operator. <, <=, >, >=
  1. Give an example of a primitive date type. int, short, long, byte, float, double, char, boolean
  1. Give an example of a logical operator. &, ||, !
  1. Give an example of Conditional statement. if statement, if-else statement, switch statement.
  1. Name any class from Java class library. String, System, Random, Math, Scanner, Graphics, JApplet
  1. Which java reserved word is used to declare constant variables? final
  1. What is a compiler?

Program that translates code in one language to equivalent code in another language.

  1. 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.

  1. 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;

}

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?

1

3

5

7

9

11

13