Midterm Practice Problems

Similar documents
Midterm Practice Problems - answer key Answers appear in boldface.

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Logic & program control part 2: Simple selection structures

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

AP Computer Science A

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS111: PROGRAMMING LANGUAGE II

1 Short Answer (5 Points Each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Repetition Algorithms

AP CS Unit 3: Control Structures Notes

Loops. Repeat after me

Chapter 3: Program Statements

1 Short Answer (10 Points Each)

Java Classes: Random, Character A C S L E C T U R E 6

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Level 3 Computing Year 1 Lecturer: Phil Smith

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

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

Oct Decision Structures cont d

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CS1004: Intro to CS in Java, Spring 2005

5) (4 points) What is the value of the boolean variable equals after the following statement?

Logical Operators and switch

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Haskell Types COMP360

Decisions in Java IF Statements

Repetition, Looping. While Loop

LECTURE 04 MAKING DECISIONS

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

1 Short Answer (5 Points Each)

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

1. An operation in which an overall value is computed incrementally, often using a loop.

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Chapter 4: Making Decisions

Control Structures II. Repetition (Loops)

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

Conditionals and Loops

Chapter 4: Making Decisions

Introduction to Computer Science Unit 2. Notes

Control Structures: if and while A C S L E C T U R E 4

Lesson 10..The switch Statement and char

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Java Flow of Control

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

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

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Darrell Bethea May 25, 2011

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CSE 142, Summer 2010 Midterm Exam, Friday, July 30, 2010

Final Exam. COMP Summer I June 26, points

Java Classes: Math, Integer A C S L E C T U R E 8

CS150 - Sample Final

Principles of Computer Science I

Microsoft Visual Basic 2005: Reloaded

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Accelerating Information Technology Innovation

CS Week 5. Jim Williams, PhD

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

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Case by Case. Chapter 3

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

Unit #3: Recursion, Induction, and Loop Invariants

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Write code that simulates one roll of a 6-sided die and prints out the

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

STUDENT LESSON A12 Iterations

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

I. True/False: (2 points each)

Midterm Examination (MTA)

Tutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I

1 Short Answer (15 Points Each)

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Computational Expression

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

PROGRAMMING FUNDAMENTALS

Loops. CSE 114, Computer Science 1 Stony Brook University

Control Structures in Java if-else and switch

Final Examination Semester 2 / Year 2011

CSIS-120 Final Exam Fall 2015 Name:

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

ECE 122. Engineering Problem Solving with Java

Conditionals, Loops, and Style

Program Fundamentals

Write a program which converts all lowercase letter in a sentence to uppercase.

QUIZ: What value is stored in a after this

Ex: If you use a program to record sales, you will want to remember data:

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Transcription:

Part 1 read-code problems. Show your work for partial credit (a) Consider the following code segment: int p = 30; int b; System.out.print( Please enter an integer ); b = kb.nextint(); if (b > p b < 0 ) { p = 2*p; b = b/25; p = 0; b = b%2; Assume the user entered 55. What is the value of variable p after the execution of this program segment? What is the value of variable b after the execution of this program segment? (b) What will be printed by the following code segment? int a = 35; int b = 100; boolean flag = false; if (!flag a > b ) System.out.println ( One ); else System.out.println ( Two ); (c) Consider the following code segment: String mystr = kb.nextline(); int len = mystr.length(); String result; if (len <=3 mystr.charat(0) == Z ) { result = RARE ; result = NOT RARE ; Page 1 of 5

Show values of variables len and result after the execution of this program segment for the following user inputs: user enters Zanzibar len result user enters Moo len result (d) What will be printed by the following code segment? int a = 20; int b = 100; boolean flag = false; if ( b % a == 0 ) flag = true; if (!flag ) { System.out.println ( One ); System.out.println ( Two ); Show values of following expressions: b % a!flag (e) Consider the following code segment and show what is printed when it is executed. Show the intermediate values of variables k and s for partial credit. int k = 1; int s = 0; while ( s < 5 ){ System.out.print( * ); if (k % 2 == 1) s = s + k; k++; System.out.println(k); System.out.println(s); Part 2 write short code segment problems. (a) Rewrite the following switch statement using if and/or if-else statements. switch (str.charat(0)) { Page 2 of 5

case S : case O : num = 1; break case T : num = 10; break; default: System.out.println ( Error. ); (b) Complete the following code segment so that it would print the first and the last character of mystr. String mystr = kb.nextline(); (c) Compose a loop that will compute the sum of all multiples of 3 between 200 and 250 (d) Complete the next segment so that it calculates the monthly interest on the amount of dollars stored in variable cost, corresponding to the yearly interest rate stored in yearlyrate. double cost = kb.nextdouble(); double yearlyrate = kb.nextdouble(); (e) Rewrite the following for loop as an equivalent while loop: for (int p = 0; p<=str.length() 1; p++) System.out.println(str.charAt(0)); Page 3 of 5

(f) Given a word that contains letter a, e.g. flubbergasted produce one that has two letters after the first a capitalized, i.e. flubbergasted. (g) Assume lines is an array of Strings. Write a code segment that calculates how many elements in lines have length between 3 and 15. Part 3 compose a program. 1. In this problem you will develop a main method that prints out a character corresponding to a grade value that is entered by the user. The following chart describes the correspondence between a numeric grade and its letter equivalent. Letter grade 90 <= grade A 80 <= grade < 90 B 70 <= grade < 80 C 60 <= grade < 70 D grade < 60 F 2. In this problem you will develop a main method that prints value true if in a word that is entered by user, the first character is the same as the last character, and false otherwise. Your program should be casesensitive, i.e. consider the capital letters different from lowercase. For example, when passed the word Halloween, the program must print false because the first character is H and the last is n, but when passed the word classic, it must print true, since both the first and the last characters are c. 3. Write a complete program that computes the number of A s on the test given a list of grades and checks if the grades are entered in the non-decreasing order. The program must at first read the number of grades in the list (n) followed by exactly n numeric grade values. The program must compute and print 1. how many grades were greater or equal to 90, 2. whether or not the grades were entered in non-decreasing order, i.e. if each next grade was equal to or greater than the previous. Page 4 of 5

For example, given the following user input: 5 80 70 90 95 76 (where 5 is the number of grades, and the grades are 80, 70, 90, 95, and 76) the program must print There were 2 A-s, The grades were not entered in non-decreasing order. since there were only two grades equal or greater than 90, and the second grade (70) was lower than the one before it (80). Here s another example. For user input 5 80 82 90 90 99 the output should be There were 3 A-s, The grades were entered in non-decreasing order. Your program doesn t have to contain comments, but must otherwise use good programming style. 4. Write a complete program that lets a user enter words, computes how many words in the input have the same starting and ending letter. The program must at first read the number of words in the list (n) followed by exactly n words each appearing on a separate line. Then, the program must compute and print how many words start with the same letter as they end with, For example, given the following user input: 5 Shine on you crazy diamond (where 5 is the number of words that follow) the program must print Number of words with matching first and last letter is 1 since there is only one word (diamond) with first letter matching the last, and the word on, for example, is shorter than the previous word Shine. Here s another example. For user input 3 one seven eighteen the output should read Number of words with matching first and last letter is 0 Your program doesn t have to contain comments, but must otherwise use good programming style. Page 5 of 5