{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Midterm2 Study Review With Answers

Midterm2 Study Review With Answers -...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: http://WWW-rohan.sdsu.edu/~masc0500/LECS107/f08mt235.txt MIDTERM #2 STUDY SHEET C8107, FALL 2008 The Midterm questions will be similar to those given below. Note: The answers to the sample questions are included at the end of this Study Sheet. Try the sample problems, then check their answers to see how you might have performed on an actual exam. If you miss a problem, study the related lecture notes. NOTE: On the exam, any user—input program code will be given to you. This will allow students to practice for the exam by using whichever user—input commands they like best, i.e. "sdsu.io” or "java.io" or "Scanner", without being at a disadvantage on the actual exam. (On the exam, "sdsu.io" code will be used because it's the simplest.) BASIC PROGRAMMING #1. Fill in the blanks, to complete the following Java program. It should ask the user to input three test scores (assume each will be an integer between 0 and 100). Then, it should calculate and display the average of the three scores. import sdsu.io.*; public class Progl { public static void main(String args) { int a = Console.readInt("Enter test score #1, from O to 100 "); int b = Console.readInt("Enter score #2, from 0 to 100 "); int c = Console.readInt("Enter score #3, from O to 100 "); X = / ; System.out.println("The average is: " ); BASIC PROGRAMMING #2. 1 of8 12/11/2008 3:12 PM 20f8 http://www-rohan.sdsu.edu/~masc0SOO/LECS107/f08mt255.txt ' Fill in the blanks, to create a program fragment (not the whole program) which does the following. Suppose that the variable timel stores the time (in seconds) that a runner needed, to run around a track. Suppose also that the variable time2 stores the time (in seconds) that the runner needed, to run around the track on a later date. Write a short Java program fragment which displays the message "Became faster by ## seconds" if the second run was faster than the first, or displays "Same time" if the second time was the same as the first, or displays "Became slower by ## seconds" if the second run was slower than the first. (Of course, in the actual output, the ##‘s above should be replaced by the actual number of seconds of difference.) Hint: pay attention to positive and negative values, so that the output makes sense. For example: * If timel is 10, and time2 is 18, then your program code should cause the output to be: "Became slower by 8 seconds", and NOT "Became faster by —8 seconds" and NOT "Became slower by —8 seconds". * If timel is 20, and time2 is 10, then your program code should cause the output to be: "Became faster by 10 seconds", and NOT "Became slower by —10 seconds" or etc. int timel = Console.readInt(”Enter time #1: "); int time2 = Console.read1nt("Enter time #2: "); int a; a = i System.out.println("Became faster by " + a + " seconds"); System.out.println("Same time"); System.out.println("Became slower by " + a + " seconds"); NESTED ”IF" STRUCTURES #3. What will be the output generated to the screen upon running the following program? public Class Progl { public static void main (String args) { int a = 1; 12/11/2008 3:12 PM 3of8 http://www-rohan.sdsu.edu/~mascOSOO/LECS107/fl)8mt255.txt if (a < O) { if (a < 10) { System.out.println("0ne"); } } else if (a == 1) { System.out.println("Two"); } else if (a >= 1) { System.out.print1n("Three"); } NESTED "IF" STATEMENTS #4. What will be the output generated to the screen upon running the following program? public class Progl { public static void main (String args) { int a = 8; int 13 = 10; if (b < a) { if (a + 2 >= b) { System.out.printan"Joe"); } else { System.out.println("Jane"); } . } else { if (a + 2 <= b) { System.out.println("John"); } else { . System.out.print1n("Je551e"); } } } } 12/11/2008 3:12 PM http://www-rohan.sdsu.edu/~masc0500/LECS107/fl) 8mt253.txt "FOR" LOOPS #5. What will be the output generated to the screen upon running the following program? public class Progl { public static void main (String args) { for (int 1:16; i >= 6; i=i-2) { } System.out.println(5*i); "WHILE" LOOPS #6. What will be the output generated to the screen upon running the following program? public class Progl { public static void main (String args) { int y = 6; int x = 1; while (x <= y+2) { x = x + 2; System.out.print1n(y); Y=y-1; } } } "DO WHILE" LOOPS [[[ *** THIS ONE WILL NOT BE ON MIDTERM #2 *** 1]] #7. What will be the output generated to the screen upon running the following program? public class Progl { public static void main(String args) { 4 of8 12/11/2008 3:12 PM http://www-rohan.sdsu.edu/~masc0500/LECS107/f08mt255.txt H :3 H w I m H Y= -2; System.out.println(y); x = x + 3; } while ( 2*x <= y+l ); "FOR" LOOPS #8. What will be the output generated to the screen upon running the following program? public class Progl { public static void main(String args) { for (int x=l ; x<=4 ; x++) { int y = 10*X + l; System.out.println("x=" + X + ", y=" + y); } } The exam will have some questions that ask you to "fill in the blanks" to write a program. THE EXAM'S PROGRAMMING QUESTIONS MIGHT NOT BE IDENTICAL OR SIMILAR TO THE SAMPLE QUESTION BELOW. Therefore, don't necessarily focus on memorizing the following specific problem and the specific answers. Instead, concentrate on becoming so familiar with Java's commands (discussed in lecture) that you can combine them to solve any problem task you are given, even ones you haven't seen before. (This will require regular practice, as with sports or music.) SAMPLE PROGRAMMING QUESTION #9. 5 of8 12/11/2008 3:12 PM http://www—rohan.sdsu.edu/~mascOSOO/LECS107/fOSmt25s.txt Write a program that will do the following. * The program should ask the user to input an odd—numbered integer greater than 10. (An "odd number" is a number that isn't evenly divisible by 2.) * For ease, assume that the user will always input an integer, and will never input a double or a String. * Your program should compute and output the sum of all odd numbers starting at 11 and ending at the user's inputted value. * Note: But, if the user's inputted value doesn't meet the requirement of being greater than 10, then the program should ONLY output the message "Your number isn't greater than 10". * Note: If the user's inputted Value doesn't meet the requirement of being an odd number, then the program should ONLY output the message "Your number is even, not odd". Example: if the user inputs 21, then the program should compute the sum 11+13+15+17+19+21, which is 96. So, the program should output 96. Example: if the user inputs 11, then the program should compute the sum 11, which is 11. So, the program should output 11. Example: if the user inputs 10, then the program should only output the message ”Your number isn't greater than 10". Example: if the user inputs 16, then the program should only output the message "Your number is even, not odd". (Reminder: the exam's programming questions will be fill—in—the—blanks. But, for practice, try to do this one on your own. If you get stuck, the answers are included below.) END OF PRACTICE EXAM. ANSWERS ARE INCLUDED BELOW. #1. ANSWER: double x = (a+b+c) / 3.0 { System.out.println("The average is: " + x ); Note: parentheses are used when calculating the average, so that the computer will perform the mathematical operations in the proper order. (Refer to the lectures regarding operator precedence rules, i.e. PEMDAS.) Note: 3.0 is used instead of 3, because "integer—divided—by—integer" division loses any fractional decimal part, which often gives the wrong answer. This is true in almost all computer languages. So, one way to avoid that problem here, is to use integer~divided—by—real, or real—divided—by-integer, or real—divided—by—real. Above, integeredivided-by—real was used. Some alternative solutions are: double X = (double)(a+b+c) / 3; double x = (a+b+c) / (double)3; double x = (a+b+c) / 3D; 6 of8 12/11/2008 3:12 PM http://www-rohan.sdsu.edu/~masc05 OO/LECS l 07/f08mtzss.txt ’ “ float x = (a+b+c) / 3F; float x = (f10at)(a+b+C) / 3: or etc. Note: the variable "x" was declared as a "double" or a "float", because x stores an average, and averages often have a fractional decimal portion. That means x must be given a data type that can handle fractional decimal portions. Therefore, double and float are the only meaningful choices. #2. ANSWER: One of various possible solutions is: int a; if (time2 < timel) { a = timel — time2; System.out.println("Became faster by " + a + " seconds"); } else if (time2 == timel) { System.out.println("Same time"); } else { a = time2 — timel; System.out.println("Became slower by " + a + " seconds"); } #3. ANSWER Two #4. ANSWER John #5. ANSWER 80 70 60 50 40 30 #6. ANSWER 6 5 4 #7. ANSWER 70f8 12/11/2008 3:12PM L I 7 http://www-rohan.sdsu’.edu/~masc0500/LECS107/f08mt255.txt X ' 19 17 15 #8 ANSWER x=1, y=11 x=2, y=21 x=3, y=31 x=4, y=41 #9. ANSWER: import sdsu.io.*; public class Progl { public static void main(String args) { int n = Console.readInt("Input an odd—numbered integer, greater than 10: "); if (n <= 10) { System.out.print1n("Your number isn't greater than 10"); } else if (n%2 == 0) { System.out.println("Your number is even, not odd"); } else { int calc = O; for (int i=11; i<=n; i=i+2) { calc = calc + i; } System.out.println(calc); } } } End Of File 80f8 12/11/2008 3:12 PM ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern