Answer Key
University
Stony Brook UniversityCourse
CSE 114 | Introduction to Object-Oriented ProgrammingPages
10
Academic year
2023
Riley
Views
40
CSE 114 Midterm1- Practice Version Solution Spring 2022 Question Value Score 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 5 20 6 21 10 22 12 23 15 Total 100
1. A method should always return a value. (2 points) a) TRUE b) FALSE 2. A logical error in a program can be detected by a compiler. (2 points) a) TRUE b) FALSE 3. The logical operator ‘!’ has higher precedence over the arithmetic operator ‘*’ (2 points) a) TRUE b) FALSE 4. Following statement in Java is valid: int x = 2 + 'a' ; (2 points) a) TRUE b) FALSE 5. Following statement in Java is valid: int x = 2 + true ; a) TRUE b) FALSE (2 points) 6. Following statement in Java is valid: double x = 2; (2 points) a) TRUE b) FALSE 8. Following statement in Java is valid: float x = 2.0; (2 points) a) TRUE b) FALSE 9. List any two keywords from Java programming language. (2 points) Solution: public, static
10. What is the difference between the operator “=” and “==” in Java? (2 points) Solution: “=” is the assignment operator “==” is the equality operator. 11. (4 points) Show the output of the following code: public class Simple { public static void main(String[] args ) { int a = 6; int b = a ++; System. out .println( a ); System. out .println( b ); a = 6; b = ++ a ; System. out .println( a ); System. out .println( b ); } } Solution: 7 6 7 7 12. (4 points) Show the output of the following code: public class Simple { public static void main(String[] args ) { double x = 1.0; double y = 5.0; double z = x -- + (++ y ); System. out .println( x ); System. out .println( y ); System. out .println( z ); } } Solution: 0.0 6.0 7.0
13. (4 points) Show the output of the following code: public class Simple { public static void main(String[] args ) { int x = 10; int y = 4; if ( x < 5 && ++ y == 5) System. out .println( "x is : " + x + " and, y is: " + y ); else } } System. out .println( "x is : " + x + " and, y is: " + y ); Solution: x is : 10 and, y is: 4 14. (4 points) Show the output of the following code: public class Simple { public static void main(String[] args ) { int x = 10; int y = 4; if ( x < 5 || ++ y == 5) System. out .println( "x is : " + x + " and, y is: " + y ); else } } System. out .println( "x is : " + x + " and, y is: " + y ); Solution: x is : 10 and, y is: 5 15. (4 points) What would be the output for the following code segment: public class Problem { public static void main(String[] args ) { boolean x = 1 + 4 * 4 < 5 * (4 - 3) - 1 && 4 - 3 < 5; System. out .println( x ); } } Solution: false
16. (4 points) What will be value of j and sum after this code sequence is executed? public class Problem { public static void main(String[] args ) { int sum = 0; int j = 17; while ( j %10 != 0) { sum += j ; j ++; } System. out .println( "j:-->" + j + ", sum:" + sum ); } } Solution: j:-->20, sum:--> 54 17. (4 points) Show the output of the following code: when the user provides the following inputs: input an integer in the range 1 to 4 : 1 input an integer in the range 1 to 4 : 2 input an integer in the range 1 to 4 : 3 input an integer in the range 1 to 4 : -10 import java.util.Scanner; public class Simple { public static void main(String[] args ) { System. out .println( "input an integer in the range 1 to 4" ); Scanner stdin = new Scanner(System. in ); int intInput = stdin .nextInt(); switch ( intInput ) { case 1: System. out .println( "you entered ONE" ); break ; case 2: System. out .println( "you entered TWO" ); case 3: System. out .println( "you entered THREE" ); break ; default : System. out .println( "your input out of range" ); } stdin .close(); } } Solution:
you entered ONE you entered TWO you entered THREE you entered THREE your input out of range 18. (5 points) The following program prints the multiple of 5 till 50. Rewrite the following program using a for loop: public static void printFiveMultiple() { int x = 5; while ( x <= 50) { System. out .println( x ); x += 5; } Solution: public static void printFiveMultiple() { for ( int x = 5; x <= 50; x += 5) System. out .println( x ); } 19. (5 points) Rewrite the following program using switch statement. import java.util.Scanner; public class MyClass { public static void main(String[] args ) { Scanner stdin = new Scanner(System. in ); System. out .println( "enter an integer: 1, 2, 3 or 4" ); int x = stdin .nextInt(); if ( x == 1) System. out .println( "Freshman" ); else if ( x == 2) System. out .println( "Sophomore" ); else if ( x == 3) System. out .println( "Junior" ); else if ( x == 4) System. out .println( "Senior" ); else System. out .println( "Imporper input" ); stdin .close(); } } Solution: import java.util.Scanner;
public class MyClass { public static void main(String[] args ) { Scanner stdin = new Scanner(System. in ); System. out .println( "enter an integer: 1, 2, 3 or 4" ); int x = stdin .nextInt(); switch ( x ) { case 1: System. out .println( "Freshman" ); break ; case 2: System. out .println( "Sophomore" ); break ; case 3: System. out .println( "Junior" ); break ; case 4: System. out .println( "Senior" ); break ; default : System. out .println( "Imporper input" ); } stdin .close(); } } 20. (6 points) Write down a program to find out the greatest common divisor for two integers. Solution: import java.util.Scanner; public class MyClass { public static void main(String[] args ) { Scanner stdin = new Scanner(System. in ); int x , y , result ; System. out .println( "enter the first integer: " ); x = stdin .nextInt(); System. out .println( "enter the second integer: " ); y = stdin .nextInt(); result = Math. min ( x , y ); stdin .close(); while (!(( x % result == 0)&&( y % result == 0))) { result --; } System. out .println( "the greatest common divisor is: " + result ); } } 21. (10 points) Write a program that simulates rolling of a pair of dice. You can simulate rolling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number you pick represents the number on the die after it is rolled. Your program should report the number showing on each die as well as the total roll. For example:
The first die comes up 3 The second die comes up 5 Your total roll is 8 Solution: public class Midterm1 { public static void main(String[] args ) { int dice1 , dice2 ; dice1 = 1+ ( int )(Math. random ()*6); dice2 = 1+ ( int )(Math. random ()*6); System. out .println( "First die comes up " + dice1 ); System. out .println( "Second die comes up " + dice2 ); System. out .println( "Your total roll is " + ( dice1 + dice2 )); } } 22. (12 points) Write a program that prompts the user to enter two strings and displays the largest common prefix of the two strings. Here are some sample runs: Enter the first string: Welcome to C++ Enter the second string: Welcome to programming The common prefix is Welcome to Enter the first string: Atlanta Enter the second string: Macon Atlanta and Macon have no common prefix. Solution: import java.util.Scanner; public class MyClass { public static void main(String[] args ) { Scanner stdin = new Scanner(System. in ); String st1 , st2 , st3 ; System. out .println( "enter the first string: " ); st1 = stdin .nextLine(); System. out .println( "enter the second string: " ); st2 = stdin .nextLine(); st3 = "" ; stdin .close(); int len = Math. min ( st1 .length(), st2 .length()); int i = 0; while ( i < len ) { if ( st1 .charAt( i ) == st2 .charAt( i )) { st3 += st1 .charAt( i ); i ++; } else
break ; } prefix" ); if ( st3 .equals( "" )) System. out .println( st1 + " and " + st2 + " have no common else } } System. out .println( "the common prefix is: " + st3 ); 23. (15 points) Write down a complete program to generate random string. It should call a method randomString(). The method should accept an integer parameter ‘len’ and a character parameter ‘ulCase’ from the user as its arguments in the parameter list. The integer parameter ‘len’ will be used by the randomString() method to generate a string of length ‘len’, whereas, the parameter ‘case’ will determine whether the generated string is uppercase or lowercase. If ulCase’ = ‘u’ generate random uppercase string. If case = ‘l’ generate a lowercase random string. Please use the appropriate method signature. Solution: import java.util.Scanner; public class MyClass { public static void main(String[] args ) { int len ; char ulCase ; Scanner stdin = new Scanner(System. in ); System. out .println( "enter the length of string:" ); len = stdin .nextInt(); if ( len > 0){ System. out .println( "enter the case input:" ); ulCase = stdin .next().charAt(0); if (( ulCase != 'u' ) && ( ulCase != 'U' ) && ( ulCase != 'l' ) && ( ulCase != 'L' )) !" ); else System. out .println( "improper case input try again System. out .println( "random string: " + randomString ( len , ulCase )); } else len" ); } System. out .println( "please enter a positive greater than 0 stdin .close(); public static String randomString( int len , char ch ) { String randString = "" ; char inputChar ; if ( ch == 'u' || ch == 'U' ) { for ( int i = 0; i < len ; i ++) {
'A' )*Math. random ()); } inputChar = ( char )( 'A' + ( int )( 'Z' - randString += inputChar ; } else if ( ch == 'l' || ch == 'L' ) { for ( int i = 0; i < len ; i ++) { inputChar = ( char )( 'a' + ( int )( 'z' - 'a' )*Math. random ()); } randString += inputChar ; } return randString ; } }
CSE 114 Midterm 1- Practice Version Solution
Please or to post comments