CS110: Namita Singla Sample Exam1 CS110 Introduction to Computing in Java Spring 2005 Sample Exam 1 Namita Singla Feb 15, 2005 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 4 CS110: Namita Singla Sample Exam1 Page 2 of 4 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 } } CS110: Namita Singla Sample Exam1 Page 3 of 4 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 CS110: Namita Singla Sample Exam1 Page 4 of 4 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?