Intro to Programming in Java Practice Midterm

Similar documents
Computational Expression

PROGRAMMING FUNDAMENTALS

Chapter 2: Basic Elements of Java

A+ Computer Science -

CS 101 Fall 2006 Midterm 1 Name: ID:

1 Short Answer (10 Points Each)

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

Oct Decision Structures cont d

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

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

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

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

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

Midterm Examination (MTA)

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

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

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

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Loops. CSE 114, Computer Science 1 Stony Brook University

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

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

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

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Array. Array Declaration:

I. True/False: (2 points each)

Chapter 4: Control Structures I

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

Practice Midterm 1 Answer Key

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

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

BM214E Object Oriented Programming Lecture 4

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Flow of Control: Loops. Chapter 4

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

Java Bytecode (binary file)

Flow of Control. Chapter 3

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

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

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

I. True/False: (2 points each)

Getting started with Java

CS 251 Intermediate Programming Java Basics

COMP String and Console I/O. Yi Hong May 18, 2015

Chapter 5 Lab Methods

Darrell Bethea May 25, 2011

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Chapter 3. Selections

Flow of Control. Chapter 3

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Full file at

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Introduction to Java (All the Basic Stuff)

AP CS Unit 3: Control Structures Notes

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Type Conversion. and. Statements

Using Java Classes Fall 2018 Margaret Reid-Miller

Give one example where you might wish to use a three dimensional array

Fundamentals of Programming Data Types & Methods

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

CompSci 125 Lecture 11

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Programming Language. 0 A history

Lecture Set 2: Starting Java

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

Input. Scanner keyboard = new Scanner(System.in); String name;

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

In this chapter, you will:

Lecture Set 2: Starting Java

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Ch. 6. User-Defined Methods

Last Class. While loops Infinite loops Loop counters Iterations

4. If the following Java statements are executed, what will be displayed?

Mr. Monroe s Guide to Mastering Java Syntax

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CEN 414 Java Programming

I. True/False: (2 points each)

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

CS 231 Data Structures and Algorithms, Fall 2016

4. Java language basics: Function. Minhaeng Lee

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

First Java Program - Output to the Screen

Robots. Byron Weber Becker. chapter 6

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

H212 Introduction to Software Systems Honors

Transcription:

600.107 Intro to Programming in Java Practice Midterm This test is closed book/notes. SHORT ANSWER SECTION [18 points total] 1) TRUE/FALSE - Please circle your choice: Tr for true, Fa for false. [1 point each = 9 total] Tr Fa a) In Java, to declare a primitive variable that could hold a real number such as 13.243 we can write "double number;". Tr Fa b) Java programs are compiled into machine code level binary files. Tr Fa c) Java bytecode files (*.class) are interpreted and executed in the same step. Tr Fa d) We must import the Scanner class from the java.util package in order to use it in a Java program. Tr Fa e) We can put a float type value into an int type variable without explicit casting. Tr Fa f) If a programming language doesn t have at least one way to specify loops, it is not a general purpose programming language. Tr Fa g) Java uses sequence as the default order of statement execution. Tr Fa h) Counter controlled loops are best implemented with a do/while loop in Java. Tr Fa i) Repeating until the user enters "no" is an example of a sentinel controlled loop. 2) Circle only the legal (will compile) identifiers below. [3 points] lang 2faced bit_ly boolean MyVar do4others

Page 2 of 7 2) Write numbers to indicate the relative ordering of these programming phases. [4 points] _3 testing & debugging 2 write (JAVA) program code 1 define & analyze the problem 4 maintain & upgrade 4) What are two good reasons for creating sample runs during the first phase of programming? [4 pts] 1) Testing the program in the future 2) Creating a skeleton/blueprint for UI when the program is being written 3) Making sure you know how to solve the problem Code Tracing: After each code segment below, write down the exact output it would produce in the box provided. Show work for partial credit. Assume the following variables have been defined for use in each segment. Also assume that each segment is independent of the others. Warning: the indentation is purposely misleading in some cases. [58 points total] int inta, intb; double db1, db2; char ch; // 4) ------------------------------------------------------------------------------------------------------------ 5 points inta = 13; intb = 2; db1 = (inta + 4) / -intb; db2 = inta / 2.0; inta = 17 % 4; System.out.println(intA + " " + intb + " " + db1 + " " + db2); 1 2-8.0 6.5

Page 3 of 7 // 5) ----------------------------------------------------------------------------------------------------------- 3 points if ( 13 + 5 <= 15 'A' + 3 < 'F') System.out.print(" one "); System.out.print(" two "); one three System.out.print(" three "); // 6) ----------------------------------------------------------------------------------------------------------- 4 points if ( "word".length() == 4 &&! (Math.ceil(2.222) == 2)) { System.out.print(" $$ "); if ( 'M' < 'a' ) System.out.print(" ## "); $$ ## System.out.print(" ++ "); // 7) ----------------------------------------------------------------------------------------------------------- 4 points boolean a = true, b = true; if (a! b) System.out.println("ab"); if (! (a && b) ==!a!b ) { System.out.println("cd"); if (! a && b) System.out.println("ef"); ab cd gh System.out.println("gh");

Page 4 of 7 // 8) ----------------------------------------------------------------------------------------------------------- 5 points inta = 5; db1 = (double) -10 + Math.floor(3.995) * 2 / inta; System.out.printf("result is %.4f", db1); result is -8.8000 // 9) ----------------------------------------------------------------------------------------------------------- 5 points inta = 15; intb = 3; db1 = 5; do { System.out.println( inta + " " + db1 ); db1 = db1 + intb; 15 5.0 13 8.0 3 inta = inta - 2; while ( inta > db1); System.out.println(intB); // 10) ----------------------------------------------------------------------------------------------------------- 7 points String line = "A"; for (ch = B ; ch <= C ; ch++) line = ch + line + ch + "\n"; CBAB C System.out.println(line);

Page 5 of 7 // 11) --------------------------------------------------------------------------------------------------------- 4 points String phrase = "Fall Break!"; String part = phrase.substring(2,7); inta = phrase.length(); ch = phrase.charat(1); System.out.println(intA + " " + part + " " + ch); 11 ll Br a // 12) ----------------------------------------------------------------------------------------------------------- 9 points String s = "midterm time"; inta = 11; while ( inta > 0 ) { switch (s.charat(inta)) { case 'd': case ' ': System.out.print(" a "); break; case 'e': System.out.print(" b "); case 't': System.out.print(" c "); break; case 'r': System.out.print(" d "); case 'm': System.out.print(" e "); break; case 'I': case 'y': default: System.out.print(" + "); inta = inta - 2; b c + a d e c +

Page 6 of 7 // 13) ----------------------------------------------------------------------------------------------------------- 12 points for (inta = 1; 10 > inta ; inta *= 2) { System.out.print(intA); for (intb = 5; intb < 10 ; intb += inta) System.out.print( o ); System.out.println(); 1 o o o o o 2 o o o 4 o o 8 o Code Writing: write exact Java code to solve each problem as described. [24 points total] 14) Assume that variable word has been declared as a String and initialized with user input. This Java statement is supposed to output whether or not the string stored in variable word contains the character 'p'. However, it contains three errors. What are they and how would you fix them? [3] if (word.substring('p') >= 0); //should be INDEXOF //Should also not have semi colon System.out.println("p is in ", word);//should be + not, System.out.println("not there");

Page 7 of 7 15) This loop is supposed to display all the even numbers (only) between 13 and 20 exclusive, on the screen, all on the same line. However, there are three errors. What are they and how would you fix them? [3] for ( int num = 13; num < 20; num++) System.out.println(num); 1) No ln needed in println 2) num should start at 14 3) num = num + 2 16) Suppose you want to read input similar to this example from the keyboard: X 20 $23.99 some sort of text The first data value will be a single character item code, then an integer quantity, followed by a price including the '$', then some more text, all separated by spaces. You must store this data in four variables: a character called "type", an integer called quantity, a real number called "price" (without the $), and for all the rest, a string called what. Write all the Java variable declarations and statements necessary to make this happen, in the main method below. Do not overly complicate this! [14] public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); String what, temp; char type; int quantity; double price; temp = keyboard.next(); type = temp.charat(0); quantity = keyboard.nextint(); temp = keyboard.next(); temp = temp.substring(1); price = Double.parseDouble(temp); what = keyboard.nextline();