CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Similar documents
McGill University School of Computer Science COMP-202A Introduction to Computing 1

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CS313D: ADVANCED PROGRAMMING LANGUAGE

APCS Semester #1 Final Exam Practice Problems

Eng. Mohammed S. Abdualal

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Reviewing all Topics this term

Programming. C++ Basics

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Fundamental of Programming (C)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Lecture 5: Making Decisions

Visual C# Instructor s Manual Table of Contents

Introduction to Programming Using Java (98-388)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Lecture 3 Tao Wang 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Full file at

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

Programming for Engineers Iteration

Computer Programming : C++

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Dept. of CSE, IIT KGP

PIC 10A. Review for Midterm I

Fundamentals of Programming

CS 106 Introduction to Computer Science I

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CpSc 1111 Lab 4 Formatting and Flow Control

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CS110D: PROGRAMMING LANGUAGE I

COMP 202 Java in one week

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Course Outline. Introduction to java

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Computer Science II Lecture 1 Introduction and Background

CSCE Practice Midterm. Data Types

5. Selection: If and Switch Controls

Logical Operators and switch

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture 5 Tao Wang 1

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Introduction to Software Development (ISD) Week 3

Control Structures in Java if-else and switch

Full file at

Computer Programming I - Unit 5 Lecture page 1 of 14

Chapter 2: Using Data

The following expression causes a divide by zero error:

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Chapter 4: Basic C Operators

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 2 Tao Wang 1

COMP Primitive and Class Types. Yi Hong May 14, 2015

Operators. Java operators are classified into three categories:

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CISC 1600 Lecture 3.1 Introduction to Processing

AP Programming - Chapter 6 Lecture

LAB 4.1 Relational Operators and the if Statement

Control Structures in Java if-else and switch

Pace University. Fundamental Concepts of CS121 1

Add Subtract Multiply Divide

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

An introduction introduction to functional functional programming programming using usin Haskell

Curriculum Map Grade(s): Subject: AP Computer Science

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Please write your name and username here legibly: C212/A592 6W2 Summer 2017 Early Evaluation Exam: Fundamental Programming Structures in Java

IT Introduction to Programming for I.T. Midterm Exam #1 - Prof. Reed Spring 2008

CIS 110: Introduction to Computer Programming

Program Elements -- Introduction

CSCE 206: Structured Programming in C++

CT 229 Java Syntax Continued

Following is the general form of a typical decision making structure found in most of the programming languages:

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

CS11 Java. Fall Lecture 1

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Simple Java Reference

INTRODUCTION TO COMPUTER SCIENCE - LAB

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Computer Programming. Decision Making (2) Loops

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

CS111: PROGRAMMING LANGUAGE II

CS 177 Recitation. Week 8 Methods

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Transcription:

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have been covered in the first 5 weeks of the course. The exam will cover most of chapters 1.1 1.4 of the textbook, and all of the lectures up through and including the Thursday of week 5 (9/28/2017). It will also cover labs 1-5 and projects 0, 1 and 2. Each of the following topics may appear on the exam. 1. Basic syntax & style Overall structure of a Java program Data types: float, double, int, char, String, arrays Variables and constants (final static) Assignment statements Arithmetic Expressions (operators for each of the data types, precedence rules, associativity) Type conversion and type casts Using Java libraries 2. Input and Output Using command line arguments. Sending output to the console (StdOut and System.out). Interactive input/output: prompting for then reading input (simple StdIn). 4. Conditional Statements Basic syntax: if, if else, if...else if else if etc. Nested conditionals Use of curly braces and proper indenting Boolean (true/false) expressions and variables Relational operators: >, <, >=, <=, ==,!= Logical operators: &&,,! 5. Switch Basic syntax: switch, case, default, and break statements. 6. Loops while loops, for loops, do-while loops controlling loops: count, event, sentinel, etc. nested loops 7. Arrays Declaring arrays, accessing array elements. Initializing arrays. Iterating over arrays. Arrays with meaningful index values. Parallel arrays.

Practice Problems The following problems are intended to help you study for the exam, however topics not covered here may be on the exam as well. Use your text, class notes, labs and assignments to review as well. The textbook site has many good review problems and contains numerous exercises with answers. 1) Which of these is a valid assignment statement (circle all correct answers): players = teama + teamb; int players = team A + team B; int players = (int) teama + (int) teamb; int = teama + teamb; 2) Assume the following prototype (heading/signature) is in the Math library: double Cube(double number); a) What does the word double at the beginning of the line refer to? b) Write a single line of code to declare a double variable named answer and assign to it the cube of 7.3. Use the above library function. c) Write a statement to find the cube of the average of x and y and print the result. Use the above library function. Assume x and y are integer variables that are already declared and initialized. 3) Compute the value of the following expressions: a) (30 / 5) % 4 + 2 b) (17-11) / 3 + 3 c) Rewrite part b, adding parentheses so the addition is performed before the division. d) What is the value of the expression in part c? 4) Write a program that does the following: Step 1) Assign the number the user entered as a command line argument to an integer variable. Step 2) If the number is less than 100, print XX is small, where XX is filled in by the number the user typed. Step 3) Otherwise, write "XX is large", with XX filled in as appropriate.

5) Given the variables and values: boolean x = true; boolean y = false; int z = 5; What is the value of each of the following Java expressions? i) x && (y (z>1)) ii)!x!(y && (z >= 5)) iii) y (!x &&!(z < 10)) 6) The following switch statement does not work as intended. Show what would be printed when it is executed assuming the user enters "5" when prompted, then fix the switch statement so that it will work properly. Also modify the program so that it prints an appropriate error message if the user types an invalid number. StdOut.print("What day? "); int day = StdIn.readInt(); switch (day) { case 1: StdOut.println("Sunday"); case 2: StdOut.println("Monday"); case 3: StdOut.println("Tuesday"); case 4: StdOut.println("Wednesday"); case 5: StdOut.println("Thursday"); case 6: StdOut.println("Friday"); case 7: StdOut.println("Saturday"); }

7) Consider the following while loop: int sum = 0; int count = 5; while (count <= 10) { sum = sum + count; count ++; } a) Before the loop, sum and count have been assigned values 0 and 5, respectively. Fill in the following table showing what values are assigned to these variables during each iteration of the loop (there may be more rows than you need in the table). sum count 0 5 b) Write a for-loop that does the equivalent to the while loop in 7a.

8) Consider the following Java source code: int a = 4; int b = 3; double c; c = a/b; a) What is the value of variable c after executing the four lines of code above? b) Rewrite the 4th line above using type-casts to make Java perform floating point division. 9) Write a snippet of Java code that compares two integer arrays (A and B) and outputs true if the arrays are "equal" (for example, if the array A contained three integers, 5 in the first position, 10 in the second position, and 15 in the third position, and the array B contained the same numbers in the same positions, the arrays would be equal, otherwise they would not be equal.) 10) Write a conditional that accomplishes the following: i) If gender is 'F' and height is greater than 70 inches, invite the user to tryout for the women's basketball team. ii) If gender is 'M' and height is greater than 75 inches, invite the user to tryout for the men's basketball team. iii) Otherwise, print out "Basketball may not be for you." Start with this code: StdOut.print("Enter your height in inches: "); int height = stdin.readint(); StdOut.print("Enter your gender (M/F): "); char gender = (stdin.readstring()).charat(0); // write your conditional(s) here. 11) Rewrite your code from question 10 so that it accepts both upper and lower case letters.

12) Write a snippet of code that compares a String variable named color and assigns either true or false to a variable, isprimary. If the color is the String red, green, or blue, the variable should be assigned true. Otherwise, it should be assigned false. 13) Write a snippet of code that assigns a double variable PartialHarmonicSum in the following way. It should use one integer variable named n, and a loop to calculate the nth harmonic number (the sum of the reciprocals of the first n natural numbers), what a mathematician would write as: 1 + 1/2 + 1/3 + + 1/n. You can assume that n is not negative. 14) Write a program that generates a table as shown below output. Avoid redundancy in your code. The first column is self explanatory. The second column is the square of the number in the first column. And the third column is the sum of all the squares up to and including that row of the table. Don't worry about aligning the columns perfectly. N N^2 sum 0 0 0 1 1 1 2 4 5 3 9 14 4 16 30 5 25 55 6 36 91 7 49 140 8 64 204 9 81 285 15) Write code that uses command-line input for two numbers, a width and a height, then prints a rectangle of plus-signs of the given size.

16) Write a snippet of code that will calculate and print the average of all the values stored in an array named grades. You can assume that grades is an array of double values that has already been initialized and contains at least one value. 17) Write a snippet of code that will calculate and print the minimum of all the values stored in an array named grades. You can assume that grades is an array of double values that has already been initialized and contains at least one value. 18) Write a snippet of code that will calculate and print how many B's a student received. You can assume the student's grades are stored in an array named grades, which is is an array of double values that has already been initialized and contains at least one value. Recall that a B is any grade between 80 and 90 (but not including 90). 19) Write a program that asks the user to input 4 integers. Store these into an array. Then print the numbers the user typed, but in reverse order. For example, if the user enters "2 4 6 8", then the program should print "8 6 4 2". 20) Show what values are stored in the array named z at the end of this snippet of code. int[] x = { 2, 4, 6, 8 }; int[] z = { 5, 10, 15, 20 }; for (int i = 0; i < x.length; i++) { if (i % 2 == 0) z[i] += x[i] + z[i+1]; else z[i] = x[i] + z[i-1]; }