Java Programming 1 (Revision) Name: Section#: ID: Q1: True/false 1. A Java identifier can start with a digit. (F) cannot 2. Hello, HELLO, and hello are considered as different identifiers. (T) 3. The name of a Java program file must end with the extension.class (F).java 4. Comments are not ignored by the compiler and they cause the computer to print the text after the // on the screen when the program executes. (F) 5. The number of arguments in the method call must match the number of parameters in the method declaration s parameter list. (T) 6. An array can store many different types of values. (F) Q2: Which of the following are valid Java identifiers? If not explain why 1. Quiz1 valid 2. first_student valid 3. Sara s_book invalid since it contains an apostrophe. 4. 2Two invalid since it starts with a digit 5. _a valid 6. Thirty_Five valid 7. char invalid since char is reserved word 8. $R3 valid 9. midterm-grade invalid since it contains dashes. Q3: Fill in the blank: 1. The programs that translate high-level language programs into machine language are called. compilers 2. Return type indicates that a method will not return a value. void 3. A variable known only within the method in which it s declared is called a(n). local variable 4. An array is a group of variables (called elements or components) containing values that all have the same. type T.A. Arwa Alromih Page 1
5. The number used to refer to a particular array element is called the element s. index 6. An array that uses two indices is referred to as a(n) array. two-dimensional 7. The name of the element in row 3 and column 5 of array d is. d[3][5] Q4: Choose the correct answer: 1. High-level programming languages are (easier, harder) to understand and program than low-level ones or assembly language. 2. Java is considered a (high level language, assembly language, low-level language). 3. Comments can be written using ( //, \\, ). 4. if (7 <= 7) System.out.println(6-9 * 2 / 6); outputs the following: i. -1 ii. 3 iii. 3.0 iv. none of these 5. if (5 < 3) System.out.println("*"); if (7 == 8) System.out.println("&"); System.out.println("$"); outputs the following: i. * ii. & iii. $ iv. none of these Q5: Show the value of x after each statement is performed: a) x = 7 + 3 * 6 / 2-1; x=15 b) x = 2 % 2 + 2 * 2-2 / 2; x=3 c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ); x=324 Q6: Give the method header for each of the following methods: 1. Method hypotenuse, which takes two double-precision, arguments side1 and side2 and returns a double-precision, result. public static double hypotenuse (double side1, double side2) 2. Method smallest, which takes three integers x, y and z and returns an integer. public static int smallest (int x, int y, int z) T.A. Arwa Alromih Page 2
3. Method instructions, which does not take any arguments and does not return a value. [Note: Such methods are commonly used to display instructions to a user.] public static void instructions () 4. Method inttofloat, which takes integer argument number and returns a float. public static float inttofloat (int a) Q7: Find the errors in the following programs and state whether it's logical or syntax error: Code // 1 error public class cs public static void main(string args[]) int a=5, b=5,c=0; if(a++>0) c++; c--; //1 error public class cs public static void main(string args[]) int a=5, b=5,c=0; Error Type syntax Correction closing after if and before if(a++>0) c++; c--; syntax if (a++>b && b >0) c++; if(a++>b>0) c++; c--; // this loop should read 11 numbers from the user int num; for(int i =0; i<10;i++) num = read.nextint(); //this loop print numbers from 10 to 0 in descending order int n= 0; while( n >= 0) System.out.println(n); n--; //2 errors int[] A=new int[10]; for(int i=0;i<a.length();i++) A(i)=i*2; public static void p( ) int a=4,b=6,c=5; return a*b*c; public static int sum(int x,int y) double result =x+y; return result; logical for(int i =0; i<=10;i++) num = read.nextint(); logical int n= 10; syntax syntax syntax for(int i=0;i<a.length;i++) A[i] = i*2; should return int return type should be double T.A. Arwa Alromih Page 3
Q8: What is the output of the following java code segment: int a = 6; if (a > 0) switch (a) case 1: System.out.println( a + 3); case 3: System.out.println( a++); case 6: System.out.println( a + 6); case 8: System.out.println( a * 8); default: System.out.println( a ); System.out.println( a + 2); double[] array = new double[4]; double mul = 1; array[0] = 2.0; array[1] = array[0]*array[0]+5-3; array[2] = array[0]+ array[1]/2; array[3] = 2.0; for (int i = 0; i < array.length; i++) System.out.print(array[i] + " \n"); mul *= array[i]; System.out.print("the multiplication is :" + mul); 12 48 2.0 6.0 5.0 2.0 the multiplication is :120.0 Q9. What is the value of x after each of the following statements is executed a) x = Math.abs(-123); 123 b) x = Math.floor(-5.1); -6 c) x = Math.ceil(-5.1); -5 d) x = Math.ceil(3.0); 3 e) x = Math.abs(-3.77); 3.77 f) x = Math.pow(8,2); 64 g) x =(-Math.abs(-8 + Math.round(4.5))); -3 T.A. Arwa Alromih Page 4
Q10: Which of the following method headings are valid? If they are invalid, explain why. a. public static int method1( ) valid b. public static char for(int a, int b) invalid, for is a reserved word c. public static void (int a, int b) invalid, missing method name d. public static double method2(double x,y) invalid missing second parameter type e. public static methodx() invalid, missing return type f. public static double method2(string ) invalid, missing parameter name. Q11: Convert the if.. statement into Conditional operator (? : ) if (a >= b) max = a; max = b; max = (a >= b)? a : b; if (rank <60) System.out.println("Rank B"); System.out.println("Rank A"); System.out.println( (rank <60)? "Rank B" : "Rank A"); Q12: Convert the while loop into for loop: x = 5; y = 35; while (x < y) System.out.println(x + " " + y); x = x + 10; x = 5; y =35 for (x=5; x<y; x+=10) System.out.println(x + " " + y); T.A. Arwa Alromih Page 5
Q13: Convert the for loop into while loop: for (i = 0; i <= 5; i++) System.out.print("*"); i=0; while (i<=5) System.out.print("*"); i++; Q14: Convert the switch statement into if.. statements: switch (grade) case 'A': System.out.println("The grade is A."); case 'B': System.out.println("The grade is B."); case 'C': System.out.println("The grade is C."); case 'D': System.out.println("The grade is D."); case 'F': System.out.println("The grade is F."); default: System.out.println("The grade is invalid."); if (grade == 'A') System.out.println("The grade is A."); if (grade == 'B') System.out.println("The grade is B."); if (grade == 'c') System.out.println("The grade is C."); if (grade == 'D') System.out.println("The grade is D."); if (grade == 'F') System.out.println("The grade is F."); System.out.println("The grade is invalid."); T.A. Arwa Alromih Page 6
Q15: Convert the following array declaration into the shorthand notation: int[] a = new int[5]; a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; a[4] = 4; int[] a = 0,1,2,3,4; int[][] a = new int[2][3]; a[0][0]=0; a[0][1]=1; a[0][2]=2; a[1][0]=3; a[1][1]=4; a[1][2]=5; a[2][0]=6; a[2][1]=1; a[2][2]=8; int[][] a =0,1,2,3,4,5,6,7,8; Q16: Programming Challenges: 1. Write a method that takes an array of ints as an argument, plus two additional int arguments called pos1 and pos2. The method should swap the elements of the array at positions pos1 and pos2. public static void swap(int [] arr, int pos1, int pos2) int temp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = temp; 2. Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers the program should stop when the user enters 0 public class sumoddeven static java.util.scanner input; public static void main(string[] args) input = new Scanner(System.in); int number = 0; // variable to store the new number int sumodds = 0; // variable to store the sum of odd integers int sumevens = 0; // variable to store the sum of even integers //ask for the user input System.out.println("Please enter integers, 0 is the sentinel value"); do //get the input number = input.nextint(); //determine whether the number is even or odd if (number % 2 == 0) T.A. Arwa Alromih Page 7
sumevens = sumevens + number; sumodds = sumodds + number; while(number!= 0); // endloop //print out the result message System.out.println("Even sum: " + sumevens); System.out.println("Odd sum: " + sumodds); 3. Write a program that reads five numbers from the user and stores them in an array, then find their sum, and print the numbers in the reverse order. import java.util.*; public class ReversePrint static Scanner input = new Scanner(System.in); public static void main(string[] args) int[] items = new int[5]; //declare array item of five elements int sum; System.out.println("Enter five integers:"); sum = 0; for (int i = 0; i < items.length; i++) items[i] = input.nextint(); sum = sum + items[i]; System.out.println("The sum of the numbers = " + sum); System.out.print("The numbers in the reverse "+ "order are: "); //print the numbers in the reverse order for (int i = items.length - 1; i >= 0; i--) System.out.print(items[i] + " "); System.out.println(); T.A. Arwa Alromih Page 8