Answer Key
University
Stony Brook UniversityCourse
CSE 114 | Introduction to Object-Oriented ProgrammingPages
12
Academic year
2023
Riley
Views
37
public class Problem { public static void main(String[] args ) { for ( int i = 1; i < 7; i +=2) { int j = 1; do { System. out .print( i * j + " " ); j ++; } while ( j <= i ); System. out .println(); } } } Solution: CSE114 Spring 2022:Midterm1 Maximum points possible: 100 Time allowed: 1 hour 20 minutes for test + 10 minutes (any technical issues that may arise during the test) = 1 hour 30 minutes. Instructions There are 24 questions in this test: 10 Questions are very short answer type carrying 2 points each. 10 Questions are short answer type carrying 4 points each. 4 Questions are medium size programming type carrying 10 points each. Questions are randomized, you are not allowed to go back to the previous question. You may login again in case of technical issues. 1. What will be the output for the following program: [4] 1 3 6 9 5 10 15 20 25 2. What will be the output for the following program: public class Problem { public static void main(String[] args ) { [4] boolean x = (3 + 4) * 3 < 5 * 4 + 3 - 5 || 12 - 3 * 4 > 5; System. out .println( x ); } } Solution: False 3. What will be the output of the following program? [4] public class Problem { public static void main(String[] args ) {
hello HELLO 5. Complete the following program that produces the pattern as given in the output in the sample run: [4] import java.util.Scanner; public class Problem { public static void main(String[] args ) { System. out .println( "please enter a positive integer:" ); Scanner stdin = new Scanner(System. in ); int x = stdin .nextInt(); if ( x > 0) { for ( int i = 0; i < x ; i ++) { - - - - - - - - } } else System. out .println( "Improper input !" ); stdin .close(); } } Sample run: int x = 20; float y = ( x /100.0); double z = 50 * y ; System. out .println( z ); } } Solution: Compiler error at float y = ( x /100.0); can’t convert double to float. 4. What will be the output of the following program? public class Problem { public static void main(String[] args ) { String st1 = "hello" ; String st2 = st1 .toUpperCase(); System. out .println( st1 ); System. out .println( st2 ); } } Solution: [4]
for ( int j = 0; j <= i ; j ++) System. out .print( "* " ); public class Problem { public static void main(String[] args ) { int x = 1, a = 3; switch ( a ) { case 1: x += 5; break ; case 2: x += 15; break ; case 3: x += 16; System. out .println(); 6. Rewrite the following program using a switch statement. public class Problem { public static void main(String[] args ) { int x = 1, a = 3; if ( a == 1) x += 5; else if ( a == 2) x += 10; else if ( a == 3) x += 16; else if ( a == 4) x += 34; System. out .println( "x:-->" + x ); } } Solution: [4] Please enter a positive integer: 5 Output: * * * * * * * * * * * * * * * [ Note: You can write only the missing statements in place of rewriting the whole program. No HARD CODING ! ] Solution:
break ; case 4: x += 34; break ; } System. out .println( "x:-->" + x ); } } Enter the value of x:12 default one Enter the value of x:3 three 8. What will be the output of the following program? [4] public class Problem { public static void main(String[] args ) { 7. What will be the output of the following program, when user inputs the values for x as 12 and 3 respectively? [4] import java.util.Scanner; public class Problem { public static void main(String[] args ) { int x ; System. out .println( "Enter the value of x:" ); Scanner stdin = new Scanner(System. in ); x = stdin .nextInt(); switch ( x ){ default : System. out .println( "default" ); case 1: System. out .println( "one" ); break ; case 3: System. out .println( "three" ); case 5: System. out .println( "five" ); break ; } stdin .close(); } } Solution: five
9. Given a number 5621 in octal number system, write down the mathematical expression to convert it into the corresponding decimal representation (of this number). [4] Solution: Solution: 11. Suppose you save $100 each month into a saving account with the annual interest rate 5%. So, the monthly interest rate is 0.05/12 = 0.00417. After the first month, the value in the account becomes: [10] 100 * (1 + 0.00417) = 100.417 After the second month, the value in the account becomes: int x = 4; int y = 5; int z ; if ( x ++ == -- y && ++ y >= x ) { System. out .println( "inside if block !" ); System. out .println( "x is :-->" + x ); System. out .println( "y is :-->" + y ); } else if (++ x == -- y || y ++ >= x ) { System. out .println( "inside else block !" ); System. out .println( "x is :-->" + x ); System. out .println( "y is :-->" + y ); } z = x ++ + y ; System. out .println( "z is :-->" + z ); } } inside if block ! x is :-->5 y is :-->5 z is :-->10 (5621) in octal = 5*8^3 + 6*8^2 + 2*8^1 + 1*8^0 = 2961. 10. What will be the output of the following program? [4] public class Problem { public static void main(String[] args ) { String st1 = new String( "Stony Brook" ); String st2 = new String( "Stony Brook University" ); System. out .println(( st1 + " University" ) == st2 ); } } false [ Note: You don't need to provide the final decimal value ] Solution:
import java.util.Scanner; public class Problem { public static void main(String[] args ) { double amount ; double annualInterest ; int numMonths ; Scanner stdin = new Scanner(System. in ); System. out .println( "Enter the amount:" ); amount = stdin .nextDouble(); System. out .println( "Enter the annual interest:" ); annualInterest = stdin .nextDouble(); System. out .println( "Enter the number of months:" ); numMonths = stdin .nextInt(); double monthlyInterest = annualInterest /(100*12.0); double finalAmount = 0; for ( int i = 0; i < numMonths ; i ++) { finalAmount = ( finalAmount + amount )*(1 + monthlyInterest ); } System. out .println( "final saving account amount:" + finalAmount ); stdin .close(); } } import java.util.Scanner; public class Problem { public static void main(String[] args ) { double price1 , price2 , weight1 , weight2 ; Scanner stdin = new Scanner(System. in ); 12. Suppose you shop for rice in two different packages. You would like to write a program to compare the cost. The program prompts the user to enter the weight and price of each package and displays the one with the better price. Here is a sample run: Enter the weight and price for package 1: 50 24.59 Enter the weight and price for package 2: 25 11.99 Package 2 has a better price [10] Solution: (100 + 100.417) * (1 + 0.00417) = 201.252 After the third month, the value in the account becomes: (100 + 201.252) * (1 + 0.00417) = 302.507 and so on. Write a program that prompts the user to enter an amount (e.g., 100), the annual interest rate (e.g., 5) and the number of months (e.g., 6) and displays the amount in the saving account after the given month. Solution:
1:" ); 2:" ); } } System. out .println( "Enter the weight and price of package weight1 = stdin .nextDouble(); price1 = stdin .nextDouble(); System. out .println( "Enter the weight and price of package weight2 = stdin .nextDouble(); price2 = stdin .nextDouble(); if ( price1 / weight1 < price2 / weight2 ) System. out .println( "Package 1 has better price" ); else System. out .println( "Package 2 has better price" ); stdin .close(); 13. A large company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week receives $200 plus 9% of $5000, or a total of $650. You have been supplied with the list of items sold by each sales person. The values of these items are as follows: [10] Item Value 1 239.99 2 129.75 3 99.95 4 350.89 Write a Java program that inputs one salesperson's items sold for last week and calculates and displays that salesperson's earnings. There is no limit on the number of items that can be sold. Solution: import java.util.Scanner; public class Problem { public static void main(String[] args ) { Scanner stdin = new Scanner(System. in ); int itemNumber ; int amount ; double grossSale = 0.0; double pay = 0.0; do { System. out .println( "Select the item #: [Valid Item# 1 to 4]" ); System. out .println( "Enter 0 to EXIT !" ); itemNumber = stdin .nextInt(); if ( itemNumber > 0 && itemNumber < 5) {
of item #1" ); of item #2" ); of item #3" ); of item #4" ); switch ( itemNumber ) { case 1: System. out .println( "enter the amount amount = stdin .nextInt(); grossSale += amount * 239.99; break ; case 2: System. out .println( "enter the amount amount = stdin .nextInt(); grossSale += amount * 129.75; break ; case 3: System. out .println( "enter the amount amount = stdin .nextInt(); grossSale += amount * 99.95; break ; case 4: System. out .println( "enter the amount amount = stdin .nextInt(); grossSale += amount * 350.89; break ; } } else if ( itemNumber != 0) System. out .println( "Invalid Item number please try again!" ); } while ( itemNumber !=0); pay = 200 + 0.09* grossSale ; System. out .println( "the pay per week is: " + pay ); stdin .close(); } } import java.util.Scanner; public class Problem { public static void main(String[] args ) { Scanner stdin = new Scanner(System. in ); 14. Dates are printed in several common formats. Two of the more common formats are 04/25/1995 and April 25, 1995. Write a program in Java that reads the date in the first format (e.g., 04/25/1995) and prints it in the second format (e.g., April 25, 1995). [10] Solution:
System. out .println( "Enter the date in the format \"mm/dd/yyyy\"" ); String date1 = stdin .next(); System. out .println( "you entered the date: " + date1 ); String date2 , year , month2 ; month2 = "" ; int month , day ; int indx1 , indx2 ; indx1 = date1 .indexOf( '/' ); indx2 = date1 .lastIndexOf( '/' ); month = Integer. parseInt ( date1 .substring(0, indx1 )); day = Integer. parseInt ( date1 .substring( indx1 + 1, indx2 )); year = date1 .substring( indx2 + 1); switch ( month ) { case 1: month2 = "January" ; break ; case 2: month2 = "February" ; break ; case 3: month2 = "March" ; break ; case 4: month2 = "April" ; break ; case 5: month2 = "May" ; break ; case 6: month2 = "June" ; break ; case 7: month2 = "July" ; break ; case 8: month2 = "August" ; break ; case 9: month2 = "September" ; break ; case 10: month2 = "October" ; break ; case 11: month2 = "November" ;
[Select all the options that apply] 16. The allowed type of expression in a switch statement of Java is/are: [2] [Select all the options that apply] 21. Given a number 453721. This can possibly be in [2] break ; case 12: month2 = "December" ; break ; } date2 = month2 + " " + day + ", " + year ; System. out .println( "date in second format: " + date2 ); stdin .close(); } } Solution: <, >, <=, >=, ==, != [any two needed.] 19. List any two logical operators in Java. [2] Solution: &&, ||, !, ^ [any two needed.] 20. List any two high level programming languages. [2] Solution: C, C++, Java, Python, Perl.. etc. [any two needed.] char short double String All the above 17. Pre-increment and pre-decrement operators (++var/--var) have higher precedence over post- increment and post-decrement operators (var++/var--), respectively. [2] True False 18. List any two relational operators in Java. [2] • return type • method name • formal parameter list (formal parameters name and their types respectively) • all the above [2] [Select all the options that apply] 15. A method signature includes:
22. How will you accept a single character from the user through the keyboard using Scanner class? [2] [Note: The input should NOT be accepted in a String variable, it should be accepted in a char variable.] Solution: Scanner stdin = new Scanner(System.in); char myChar = stdin.next().charAt(0); 23. Given below is a program that simulates rolling of a pair of dice. We can simulate rolling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number we pick represents the number on the die after it is rolled. The program reports the number showing on each die as well as the total roll. For example: [2] Sample run: The first die comes up 3 The second die comes up 5 Your total roll is 8 Comment on the correctness of the program: You may say following: • Program is correct • Program is incorrect If you think that it is incorrect then identify the problem and fix it. [ Note: You don't need to rewrite the whole program, just identify line# and provide the corresponding correct statement, only if you think that the program is incorrect .] public class Problem { public static void main(String[] args ) { int dice1 , dice2 ; dice1 = ( int )(Math. random ()*7; dice2 = ( int )(Math. random ()*7; 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 )); } } Solution : Incorrect logic on line: dice1 = ( int )(Math. random ()*7; dice2 = ( int )(Math. random ()*7; Should be fixed by using: dice1 = 1 + ( int )(Math. random ()*6; dice2 = 1+ ( int )(Math. random ()*6; • Binary number system • Decimal number system • Octal number system • Hexadecimal number system • All the above.
24. Write down a statement / statements to generate a random integer in the range from -25 to 75 (including -25 and 75). Do not write the complete program only the statement/statements for generating an integer in the required range. [2] Solution: int randInt = ( int )(Math. random ()*101) - 25;
CSE114: Midterm 1 Practice Test: Java Programming
Please or to post comments