COMP 1000 Computer Science I Exam 2 Review - PDF

Summary

This document is a review for a computer science exam. It contains various exercises, including questions about basic programming concepts like loops, arrays, and input/output (I/O), focusing on Java. The review includes the topics of the course covered in the semester, like mathematical expressions in Java, exception handling, and object-oriented programming principles.

Full Transcript

COMP 1000: Computer Science I Exam 2 Review School of Computing and Data Science Wentworth Institute of Technology Numbered as Lecture 22 for consistency. Format Exam II will be similar to the first exam. You are allowed a single 8.5x11” piece of paper with whatever notes you want on it....

COMP 1000: Computer Science I Exam 2 Review School of Computing and Data Science Wentworth Institute of Technology Numbered as Lecture 22 for consistency. Format Exam II will be similar to the first exam. You are allowed a single 8.5x11” piece of paper with whatever notes you want on it. Should be your own notes. Can use front and back. No calculators, books, laptops, phones, headsets, smartwatches, or anything besides your single page of notes. Format Kind of question topics to expect: 1. Multiple choice, Multiple Selection, and true or false questions 2. Explain a program or part of a program 3. Translate between “normal” math expressions and the Java equivalents 4. Write your own code 5. Fix incorrect code / find bugs in code 6. Fill in the blank (in a program) 7. Short Answer Format Kind of question topics to expect: 1. Multiple choice, and true or false questions 2. Explain a program or part of a program 3. Translate between “normal” math expressions and the Java equivalents 4. Write your own code 5. Fix incorrect code / find bugs in code 6. Fill in the black (in a program) Based on Exam I, make sure to review these 7. Short Answer in addition to the other materials. Content Essentially, everything we’ve covered so far this semester, including: 1. Input and output (Scanner and Print) 2. Exceptions, try-catch, try-with-resouce, hasNext() 3. Mathematical expressions in Java (order of operations, integer division, etc.) 4.if-else statements, switch-case statements 5.while , do-while , and for loops 6. Flowcharts and CA 7. Methods and scope 8. Javadoc, git, eclipse, general computing 9. Arrays and ArrayLists 10.Reading Files with Scanner (First part of File I/O) Review Exercises The following slides contain exercises that will help you prepare for the exam but beware that they may not be the same format as the exam! The exercises give you an idea of the style of questions to expect as well as the complexity These exercises are all about writing code to help remind you of the things we’ve done so far this semester Refer to exam 1 review slides (or your actual exam) if you need a reminder of the style of questions Exercise Write a program that reads in a series of positive integers and prints out the maximum value entered. The user will indicate they are finished entering numbers by entering zero or a negative integer. Answer Scanner input = new Scanner(System.in); int inputValue; int max = 0; System.out.printf("Enter positive integers, stopping with zero/ negative number: "); do { inputValue = input.nextInt(); if (inputValue > max) { max = inputValue; } } while (inputValue > 0); if (max == 0) { System.out.printf("You didn't enter any positive numbers!%n"); System.exit(0); } System.out.printf("The max was %d%n", max); Answer Scanner input = new Scanner(System.in); int inputValue; int max = 0; System.out.printf("Enter positive integers, stopping with zero/ negative number: "); do { Try-catch inputValue = input.nextInt(); hasNext if (inputValue > max) { max = inputValue; } } while (inputValue > 0); InputMismatchException if (max == 0) { System.out.printf("You didn't enter any positive numbers!%n"); System.exit(0); return } System.out.printf("The max was %d%n", max); Exercise Write a program that uses a for loop to calculate N!, given N. Ask the user for a value of N and your program should compute and print the value of N! ( = 1 * 2 * 3 * … * N). Answer Scanner input = new Scanner(System.in); int n; int factorial = 1; System.out.printf("Enter N: "); n = input.nextInt(); if (n < 0) { System.out.printf("No factorial for negative numbers!%n"); System.exit(0); } for (int i = 1; i = 0; i--) { System.out.printf("%d%n", values[i]); } Additional Problems The following are additional problems that may be helpful as you study. Exercise Implement the Questions of the Day. Exercise Write a main method that is able either read a file or get data from the user. Have it use a method(s) called readValues such that you can pass a bool (False for Ints, True for Doubles), an array of integers or an array of doubles, and a scanner object. Have the method set the values in the array. Exercise Write a method called squarePattern that prompts the user for an int and prints a square of chars using a set of nested for loops: Enter Size: 5 ***** ***** ***** ***** ***** Exercise Write a method to compute the average of all the elements in an array. In main, hard code an array of 5 elements and compute the average with your method. Wrap Up Review the previous slides and assignments Work through all the examples and exercises Check the book, if you have it, for additional exercises (with answers) Use the page of notes as a study guide to help you prepare for the exam Come see me with any questions or if you need some help understanding anything we’ve covered so far this semester

Use Quizgecko on...
Browser
Browser