Fall 2024 CS 18000 practice midterm exam.pdf
Document Details
Uploaded by Deleted User
Purdue University
2024
Tags
Related
- Lyceum International School Grade 8 Computer Science Exam PDF - July 2023
- BSc Part 1 Computer Science 2022 Past Paper (PDF) - F-3618
- Bihar STET 2023 Computer Science Paper II PDF
- Paradigms & Computer Programming Foundation Exam Paper - 2024 - PDF
- Computer Science PYTHON Book PDF for Class 12
- Chapter 3 Computer Science 12 - Federal Board PDF
Full Transcript
Username Fall 2024 CS 18000 PRACTICE MIDTERM EXAM 1 Printed Name Username Purdue ID 0 0 Carefully read all instructions below before starting the exam. This exam is worth a total of 75 possible points...
Username Fall 2024 CS 18000 PRACTICE MIDTERM EXAM 1 Printed Name Username Purdue ID 0 0 Carefully read all instructions below before starting the exam. This exam is worth a total of 75 possible points and contains a combination of fill in the blank, short answer, multiple choice, and coding questions. Once you have completed the exam, and reviewed your work, you can submit it and leave the exam room. Do not attempt to look at the work of other students. Do not attempt to show your work to other students. This will be considered academic dishonesty and handled accordingly. Electronic devices, watches, and phones should be turned off. Headphones and earbuds are strictly forbidden. If you are seen using an electronic device of any form, your test will be taken, and you will receive a zero on the exam. You cannot wear any hats, jackets, or hooded sweatshirts during the exam. You will be asked to remove them. Notes are not allowed on this exam. The only items you require are a writing utensil and an eraser. Be sure to write neatly and clearly. If using pencil, ensure that your writing is dark enough that it will be easily scanned. Handwriting that is deemed illegible will be given a zero for that question. All answers should be written in the given boxes. Answers outside of the box will not be scored. Additional paper will not be given. Utilize your space wisely. Read each question carefully and follow the directions to the best of your abilities. Follow the course coding style to the best of your ability when answering coding problems. You will not be graded on coding style. Absolutely no questions will be answered during the exam. Write your username at the top of EVERY page of this exam in the designated space. This ensures each page is uniquely identifiable. "By signing below, I pledge to uphold academic integrity during this exam. I will not give or receive unauthorized help, use unauthorized resources, or share exam information. I understand that violations will lead to academic penalties." Signature Do not open the exam until instructed to. 1 Username Complete each of the following by writing a SINGLE line of code. Assume all necessary imports are included at the top of the program. [2 points each] 1) Given the variables double x = 3.13301, int y = 24, and String fun = "This course is fun" print "This course is fun 3.1 - 24" to the terminal using printf with string format specifiers. 2) Given int x; Write a for loop that iterates from i = 0 to x in increments of the Fibonacci numbers. Fibonacci numbers are 1,1,2,3,5,8… where the nth Fibonacci number is the sum of the last two numbers in the series. 3) Given the variables int x and int y declare and initialize a string named values that is of the format x hyphen y. Example: "22 – 13" (Without spaces) 4) Given the variables String sOne = "I like coding." and String sTwo = "Math is easy." print "Coding is easy." to the terminal using substring. 5) Declare and initialize a boolean named isEven that is set to true when the integer variable myVar is even and false when it is odd. 6) Given the variables double x = 6.25 and double y = 5.81 declare and initialize an int named myVal to be equal to sum of x and y. 7) Given the variable Scanner scan = new Scanner(System.in); write the declaration of a while loop such that the conditional is true when scan contains an integer 8) Given the variables String one; String two; and String three;, use printf to print the contents of one two and three in order separated by spaces and ending with a newline character. 2 Username 9) Constructor [8 Points]: public class AccountManager { private String userName; private double balanceChecking; private double balanceSavings; private double balanceLoan; private boolean checking; private boolean savings; private boolean loan; private static final double LOAN_APR = 10.6; private static final double SAVINGS_APY = 2.2; //todo: your code here } Write a constructor for AccountManager that is public and takes a String userName and a String type, in that order, as parameters. Set the boolean values for each of checking, savings, and loan to true based on the type string. Example: A type string of "CS" would have both a checking and a savings account, but not a loan account. A type string of "CL" would have both checking and loan accounts, but not a savings account. A type string of "CLS" (or even "SLC") would have all three. Assume all type strings will be a valid combination of the 3 characters 'C', 'S', and 'L', all upper case. Set all balances to be 0.0. 3 Username 10) Method deposit [6 points]: For class AccountManager in question 9, write a public method named deposit that returns a boolean and takes a double amount and a String type as inputs, in that order. The method should check that the type passed to the method is a valid type for that account. If it is invalid return false and do nothing else. Example: A type of "C" would be invalid if checking is set to false. Assume that it will always be a valid string containing a single valid character. You may assume that amount will be a positive value. If the type is mapped to checking or savings, add the amount to the corresponding balance variable. If it is a loan, decrease the balance by the amount. (It is a payment.) If the amount would cause balance to be negative, return false and make no change to the balance. The method returns true if a balance changed. 4 Username 11) Main method [12 Points] Given the following class variables and methods: private final static String USERNAME_PROMPT = “Enter your username”; private final static String POST_PROMPT = “Number of posts to view”; private final static String ERROR_MESSAGE = “Invalid entry”; private final static String FUNNY_MEME = “Funny meme detected! LOL”; private final static String SPONSORED_CONTENT = “Sponsored content :(”; private final static String FRIEND_POST = “Your friend just posted!”; private final static String OLD_POST = “Oldie but a goodie!”; private final static String EXIT_MESSAGE = “The end, goodbye!”; private static int checkPostBrowsed(String username) {.. } private static boolean validateUsername(String username) {.. } Implement an infinitely scrolling menu, similar to a social media feed. The program should start by prompting the user to enter their username with USERNAME_PROMPT. If the user enters an invalid username, print the ERROR_MESSAGE and repeat the USERNAME_PROMPT until a valid username is entered. Usernames can be validated using the validateUsername() method. Next, prompt the user for the number of posts that want to view using POST_PROMPT. If the value is less than or equal to 0 or not an integer, print the ERROR_MESSAGE and repeat the POST_PROMPT. Then, display a number of posts to the user, one at a time, by calling checkPostBrowsed() for that user by printing the corresponding message; checkPostBrowsed() returns an int that signifies the type of post to be shown (see below). Finally, after all posts are displayed print the EXIT_MESSAGE. Return types for checkPostBrowsed()are as follows: Returned value is divisible by 3: FUNNY_MEME Returned value is divisible by 5: SPONSORED_CONTENT Returned value is neither divisible by 2 nor 7: FRIEND_POST Returned value is none of the above: OLD_POST Write your response in the given boxes on the next two pages. 5 Username 6 Username 7 Username 12) Method primeFactorization [10 Points] Write a public static method named primeFactorization that returns a String and takes int val as an argument. The method should return the String representation of the given val’s prime factorization. The prime factorization of a number is the list of the smallest prime numbers, in ascending order, that multiply together to form the given integer. For example, the prime factorization of 12 is 2 x 2 x 3. The returned String should contain the primes separated by ‘x’ characters and no additional white spaces. 8 Username 13) Main method [8 Points] Write a main method finds and prints all the numbers less than 10,000 that are equal to the sum of the factorials of their digits. For example, 145 is such a number because: 1!+4!+5!=1+24+120=145 Your program should output all such numbers, one per line. 9 Username Write the correct answer in the given box. [2 points each] 14) The maximum size of any signed binary value of length n can be expressed in terms of n as? 15) The minimum size of any signed binary value of length n can be expressed in terms of n as? Write a short answer in the given box. [3 points each] 16) In what instance might you choose to use a do-while loop over a for or a while loop? 17) Using only command line inputs. Compile and run the program contained in MyFile.Java For each of the following questions convert the given integer to binary in the given boxes. Assume that Two's Complement Representation is being used. [2 points each] 18) 22 19) -29 20) True or False: for loops will always terminate. [1 Point] 10