CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013



Similar documents
J a v a Quiz (Unit 3, Test 0 Practice)

Sample CSE8A midterm Multiple Choice (circle one)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Topic 11 Scanner object, conditional execution

Introduction to Java

Chapter 2: Elements of Java

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

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

AP Computer Science Static Methods, Strings, User Input

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Programming

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Java Basics: Data Types, Variables, and Loops

LOOPS CHAPTER CHAPTER GOALS

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Part I. Multiple Choice Questions (2 points each):

Moving from CS 61A Scheme to CS 61B Java

Chapter 3. Input and output. 3.1 The System class

AP Computer Science Java Subset

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Object Oriented Software Design

Install Java Development Kit (JDK) 1.8

Chapter 2 Introduction to Java programming

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Chapter 2 Elementary Programming

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CS 106 Introduction to Computer Science I

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Using Files as Input/Output in Java 5.0 Applications

Computer Programming I

Iteration CHAPTER 6. Topic Summary

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

JAVA ARRAY EXAMPLE PDF

Building Java Programs

Variables, Constants, and Data Types

Semantic Analysis: Types and Type Checking

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

Building Java Programs

Informatica e Sistemi in Tempo Reale

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Object Oriented Software Design

CS 111 Classes I 1. Software Organization View to this point:

Introduction to Java Lecture Notes. Ryan Dougherty

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Example of a Java program

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Programming Languages CIS 443

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

VB.NET Programming Fundamentals

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Java Programming Fundamentals

Crash Course in Java

(Eng. Hayam Reda Seireg) Sheet Java

Sources: On the Web: Slides will be available on:

Visual Logic Instructions and Assignments

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Statements and Control Flow

Computer Programming I

Building Java Programs

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Explain the relationship between a class and an object. Which is general and which is specific?

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Basics of Java Programming Input and the Scanner class

Programmierpraktikum

CS170 Lab 11 Abstract Data Types & Objects

Unit 6. Loop statements

While and Do-While Loops Summer 2010 Margaret Reid-Miller

Object-Oriented Programming in Java

Conditionals (with solutions)

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

While Loop. 6. Iteration

Lecture 1 Introduction to Java

The following program is aiming to extract from a simple text file an analysis of the content such as:

Java Interview Questions and Answers

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Programming in Java Course Technology, a part of Cengage Learning.

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Java Application Developer Certificate Program Competencies

Computer Programming I & II*

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Transcription:

Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100) 1. (3 pts each, 18 pts total) What is the value of the variable answer after each of the following statements given the declarations below. These are tricky - pay special attention to variable types! double answer = 10; double x = 4; double y = 3; int n = 2; int m = 7; a. answer += 2.5; answer = 10+2.5 = 12.5 b. answer = n % m; answer = 2 % 7 = 2 c. answer = m % n; answer = 7 % 2 = 1 d. answer = 4*n/m; answer = 4*2/7 = 8/7 = 1 e. answer = 1 + y/x; answer = 1 + 3.0/4.0 = 1.75 f. answer = n/x; answer = 2 /4.0 =.5

Oct 4, 2013, p 2 2. (1 pt each, 12 pts total) For each definition, identify the index (e.g. A, B, ) of the vocabulary word it describes. Please write clearly so your answer is unambiguous. Definitions: i. S) Variable A named location in memory for holding data. ii. Q) String A sequence of characters. iii. K) Magic Number A number that appears in code without explanation. iv. O) API The place a programmer can look at to read about a class s methods. v. M) Primitive Type A variable type which contains a only single value, e.g. int, double or char as opposed to String or Scanner vi. J) Concatenation Combining strings to form a new string. vii. T) Lexicographic The way Java compares and orders strings. (Note, I gave credit for P as well although it isn t specific to strings) viii. H) Cast Explicitly converting a value from one type to a different type. ix. I) Character A single letter, digit or symbol (e.g. ascii code). x. G) Sentinel An input value that is used to signal the end of input. xi. F) Runtime Error An error in a syntactically correct program that causes it to act differently from specification. xii. L) Initialization Setting the value of a variable for the first time, before it is used. Vocabulary Word A. Assignment B. Expression C. Boolean value D. Relational operator E. Compile time error F. Run-time error G. Sentinel H. Cast I. Character J. Concatenation K. Magic Number L. Initialization M. Primitive type N. Class type O. Application programming interface (API) documentation (Javadoc) P. Boolean operator Q. A String R. Type S. Variable T. Lexicographic

Oct 4, 2013, p 3 3. (4 pts each, 24 pts) Given the declarations: int x; int y; char c; String answer; String word1; String word2; Write a Boolean expression for the following: a. The answer begins with the letter Z answer.charat(0) == Z alternatively: answer.substring(0,1).equals( Z ) b. The mathematical expression 20 < x < 100 20 < x && x < 100 c. The character c is the letter E or e c == E c == e d. word1 does not equal word2! word1.equals(word2) e. It is not true that x and y are both less than 10!(x<10 && y<10) same as!(x<10)!(y<10) same as x>=10 y >=10 f. y is either less than 100 or more than 200. y < 100 y > 100 4. (4 pts each, 16 pts total) What is the output of the following loops? Briefly explain your answers (otherwise, no partial credit can be given if your answer is not quite right!) Loop 1: int cnt = 0; for (int j = 10; j < 15; j++) cnt++; System.out.println("cnt = " + cnt); Loop 1 Output: j executes 15-10 = 5 times, i.e. for values j = 10, 11, 12, 13, 15 cnt = 5

Oct 4, 2013, p 4 Loop 2: int cnt = 0; for (int j = 5; j < 15; j++) for (int i = 0; i < 10; i++) cnt++; System.out.println("cnt = " + cnt); Loop 2 Output: inner loop loops 10 times, outer loop loops 10 times, so cnt is incremented 100 times cnt = 100 Loop 3: int cnt = 0; for (int j = 0; j < 4; j++) for (int i = 0; i < j; i++) cnt++; System.out.println("cnt = " + cnt); Loop 3 Output: Values of i and j are j = 0: i = no loop j = 1: i = 0 j = 2: i = 0, 1 j = 3: i = 0, 1, 2 thus we have cnt = 6 Loop 4: int cnt = 0; for (int j = 0; j < 4; j++) for (int i = 0; i < 5; i++) for (int k = 0; k < 10; k++) cnt++; System.out.println("cnt = " + cnt); Loop 4 Output: j loop: 4 times, i loop: 5 times, k loop: 10 times So that cntis incremented 4x5x10 = 200 times cnt = 200

Oct 4, 2013, p 5 5. (12 pts) Write a while-loop which sums the integers from a to b (inclusive) and prints the result once at the very end. import java.util.scanner; public class LoopPractice public static void main(string[] args) Scanner in = new Scanner(System.in); System.out.print("Enter 2 integers: "); int a = in.nextint(); int b = in.nextint(); int sum = 0; while (a <= b) sum = sum + a; a++; System.out.println("sum = " + sum); Could also have done (this is better if you later on need the value of a): int sum = 0; int i = a; while( i <= b ) sum = sum + i; i++; System.out.println("sum = " + sum); 6. (18 pts total) Multiple Choice - Circle the correct answer. (Please be clear! Ambiguous or hard to read answers will be counted as incorrect.) A. (1 pt) Which one of the following errors represents a part of a program that is incorrect according to the rules of the programming language? a) Syntax errors c) Logic errors b) Run-time errors d) Out-of-memory errors B. (1 pt) Which one of the following types of statements is an instruction to replace the existing value of a variable with another value? a) Update c) Assignment b) Declaration d) Initialization Note: Will give credit for a) as well, although c) is a better answer.

Oct 4, 2013, p 6 C. (1 pt) What is wrong with the following code snippet? public class Area public static void main(string[] args) int width = 10; height = 20.00; System.out.println("area = " + (width * height)); a) The code snippet uses an uninitialized variable. b) The code snippet uses an undeclared variable. c) The code snippet attempts to assign a decimal value to an integer variable. d) The code snippet attempts to add a number to a string variable. D. (2 pts) What is the output of the following code snippet? public static void main(string[] args) String str1 = "I LOVE Willamette"; String str2 = str1.substring(4, 9); System.out.println(str2); a) I LOV c) VE Wil b) OVE W d) VE Wi E. (1 pt) What will be the output when the following code is executed double x = 2.5; double y = 0.0; if ( y!= 0 && x/y!= 1.0) System.out.println( x/y is not equal to 1 ); else System.out.println( x/y is equal to 1 ); a) x/y is not equal to 1 c) There will be no output. b) x/y is equal to 1 d) The program will crash.

Oct 4, 2013, p 7 F. (1 pt) What kind of operator is the <= operator? a) Boolean c) Inequality b) Arithmetic d) Relational G. (1 pt) Which of the following options refers to the technique of simulating program execution on a sheet of paper? a) Compiling c) Tracing b) Prototyping d) Debugging H. (1 pt) Which of the following loops executes the statements inside the loop before checking the condition? a) for c) do-while b) while d) do-for I. (2 pts) Assuming that a user enters 25 as the value for x, what is the output of the following code snippet? Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int x = in.nextint(); if (x < 100) x = x + 5; if (x < 500) x = x - 2; if (x > 10) x++; else x--; System.out.println(x); a) 27 c) 29 b) 28 d) 30 J. (1 pt) Which of the following loop(s) could possibly not enter the loop body at all? I. for loop II. while loop III. do-while loop a) I only c) II and III only b) I and II only d) I and III only

Oct 4, 2013, p 8 K. (1 pt) A loop inside another loop is called: a) A sentinel loop c) A parallel loop b) A nested loop d) A do/while loop L. (2 pts) What is the output of the following loop? for (int i = 20; i >= 2; i = i - 6) System.out.print(i + ", "); a) 20, 14, 8, c) 20, 14, 8, 2, b) 14, 8, d) 14, 8, 2, M. (2 pts) What is the first and last value of i to be printed by the following code snippet? int n = 20; for (int i = 0; i <= n; i++) for (int j = 0; j <= i; j++) System.out.println("" + i); a) 0 and 20 c) 1 and 19 e) None of the above b) 1 and 20 d) 0 and 19 N. (1 pt) What will be the output of the following code snippet? boolean token = false; while (token) System.out.println("Hello"); a) Hello will be displayed infinite times. c) No output after successful compilation. b) No output because of compilation error. d) Hello will be displayed only once. O. (2 pts) What is the output of the following code: int x = 7; int y = 9; int z = 5; x = y; y = z; z = x; System.out.println(x + " " + y + " " + z); a) 9 5 9 c) 7 9 5 b) 9 5 7 d) 7 7 7