Sample CSE8A midterm Multiple Choice (circle one)



Similar documents
AP Computer Science Static Methods, Strings, User Input

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

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

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

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

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

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

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

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Introduction to Java

Section 6 Spring 2013

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

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

(Eng. Hayam Reda Seireg) Sheet Java

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

JAVA ARRAY EXAMPLE PDF

CS170 Lab 11 Abstract Data Types & Objects

Topic 11 Scanner object, conditional execution

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

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

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

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

Chapter 3. Input and output. 3.1 The System class

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

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

Chapter 2 Introduction to Java programming

Homework/Program #5 Solutions

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

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

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

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

Iteration CHAPTER 6. Topic Summary

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

CS 106 Introduction to Computer Science I

Introduction to Programming

Basics of Java Programming Input and the Scanner class

CSE 8B Midterm Fall 2015

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

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

Building Java Programs

Introduction to Java. CS 3: Computer Programming in Java

Chapter 2 Elementary Programming

Object Oriented Software Design

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

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

LOOPS CHAPTER CHAPTER GOALS

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Programming Languages CIS 443

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

Continuous Integration Part 2

Example of a Java program

D06 PROGRAMMING with JAVA

AP Computer Science Java Subset

Using Files as Input/Output in Java 5.0 Applications

15-214: Principles of Software Construction 8 th March 2012

Install Java Development Kit (JDK) 1.8

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Arrays. Introduction. Chapter 7

Comp 248 Introduction to Programming

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

Object-Oriented Programming in Java

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

Building Java Programs

Programmierpraktikum

Visual Basic Programming. An Introduction

Data Structures Lecture 1

Java Basics: Data Types, Variables, and Loops

1. Writing Simple Classes:

Using Two-Dimensional Arrays

Chapter 2 Basics of Scanning and Conventional Programming in Java

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

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

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Moving from CS 61A Scheme to CS 61B Java

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Statements and Control Flow

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

Building Java Programs

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Week 1: Review of Java Programming Basics

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

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

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Interactive Applications (CLI) and Math

Java Crash Course Part I

AP Computer Science File Input with Scanner

For live Java EE training, please see training courses

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

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Conditional Statements Summer 2010 Margaret Reid-Miller

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

Object Oriented Software Design

Java 進 階 程 式 設 計 03/14~03/21

LAB4 Making Classes and Objects

Building Java Programs

CS 241 Data Organization Coding Standards

Some Scanner Class Methods

AP Computer Science Java Mr. Clausen Program 9A, 9B

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Transcription:

Sample midterm Multiple Choice (circle one) (2 pts) Evaluate the following Boolean expressions and indicate whether short-circuiting happened during evaluation: Assume variables with the following names and values: int moos = 49; int quacks = 8; int temp = -5; Expression 1: ((quacks % 3) == 2) && (temp > -5) (moos == (quacks+41)) 1. The Boolean expression above evaluates to which? true false 2. In the evaluation of that Boolean expression, did ANY short-circuiting of evaluation occur? YES NO 3. (1pt) What does the solitary. stand for in the Unix command below? Choose the most precise answer. cp MyStuff/J.java. (a) The current file. (b) The destination file. (c) The home directory. (d) The current directory. (e) A file created for our Lab classes. (f) The directory above the current directory. (g) The directory below the current directory. 4. (2pts) How many lines of output would be printed by the following code? for (int i = 0; i <= 40; i++) for (int j = 0; j < 3; j++) System.out.println("Ha!"); (a) 3 (b) 4 (c) 40 (d) 41 (e) 43 (f) 44 (g) 120 (h) 123 Page 1

5. (2pts) What is the result of running the following code? int x = 33; int y = 34; if (++x == y) System.out.println( x: + x + y: + y--); else System.out.println( y: + y-- + x: + x); Program Output: A) x: 34 y: 34 B) y: 34 x: 34 C) x: 33 y: 34 D) y: 34 x: 33 E) x: 34 y: 33 F) y: 33 x: 34 G) x: 33 y: 33 H) y: 33 x: 33 6. (2pts) Which of the following code fragment prints out all the numbers from [100,0] that are both multiples of 7 and multiples of 2? (a) int i = 100; while (i == 0) if ( i%7==0 && i%2==0 ) (b) int i = 100; while (i >= 0) if ( i%7==0 && i%2==0 ) (c)int i = 100; while(i == 0 && i%7==0 && i%2==0) (d)int i = 100; while(i >= 100 && i%7==0 && i%2==0) System.out.printl(i); (e) int i = 100; while (i == 0) if ( i%7==0 i%2 ==0 ) (f) int i = 100; while (i >= 0) if ( i%7==0 i%2==0 ) (g)int i = 100; while(i == 0 i%7==0 i%2==0) (h)int i = 100; while(i >= 0 i%7==0 i%2==0) Page 2

(6pts total) The following code will be used to answer the next few questions. READ IT CAREFULLY. import java.util.scanner; public class Test public static void main(string [] args) Scanner in = new Scanner(System.in); String foo = in.nextline(); if (foo.length() % 2 ==0) if (foo.indexof("!") >=0) System.out.print("Hurray "); else if (foo.charat(foo.length()-1) == '?') System.out.print("What "); System.out.print("Oh, yeah"); else int i = foo.indexof("*"); int j = foo.indexof("*", i+1); System.out.println(foo.substring(i,j)); 7. (2pts): What does this program do when the user inputs: *Java* is Fun A)Hurray Oh, yeah B) Oh, yeah C) What D) What Oh, yeah E) *Java* F) *Java G) is Fun H) * is Fun 8. (2pts): What does this program do when the user inputs: Rocks? A)Hurray Oh, yeah B) Oh, yeah C) What D) What Oh, yeah E) *Java* F) *Java G) is Fun H) * is Fun Page 3

9 (2pts): Which of the following statements MOST ACCURATELY AND PRECISELY describes the conditions under which Hurray is printed? A) The length of the entered String is even and the String ends with an exclamation point B) The length of the entered String is even and the String does not end with an exclamation point C) The length of the entered String is even and the String contains an exclamation point D) The length of the entered String is even and the String does NOT contain an exclamation point E) The length of the entered String is odd and the String ends with an exclamation point F) The length of the entered String is odd and the String does not end with an exclamation point G) The length of the entered String is odd and the String contains an exclamation point H) The length of the entered String is odd and the String does NOT contain an exclamation point 10 (2pts). Which of the following codes correctly implement a validation loop to ensure that the user enters a number between 1 and 9 inclusive? MORE THAN ONE ANSWER MAY BE CORRECT. Assume this line has already been executed: Scanner in = new Scanner(System.in); CIRCLE ALL CORRECT ANSWERS FOR FULL POINTS! A) int ans; while (ans > 0 && ans < 10) C) int ans; while (ans <= 0 && ans >= 10) E) int ans; do while(ans <= 0 && ans >= 10); B) int ans; while (ans > 0 ans < 10) D) int ans; while (ans <= 0 ans >= 10) F) int ans; do while(ans <= 0 ans >= 10); Page 4

WRITE YOUR ANSWERS ON THIS PAGE 11. (3 pts) If you want to convert Celsius to Fahrenheit, the mathematical equation is Below, write a short, legal, Java code segment to declare appropriate variables (with good style, naming convention, and appropriate type), read in the Celsius value from the user, and calculate and store the value in a variable. 12. (2pts) What is the output produced when the following code is executed? String foo = "Java!"; String bar = ""; for (int i = 0; i < foo.length(); i+=2) bar = foo.charat(i) + bar; System.out.println(bar); Output: 13. (2pts) Write a single line of legal Java code to declare an array of 25 ints named foo. Answer: Page 5

WRITE YOUR ANSWER ON THIS PAGE 14. (3pts) Explain what the following code does in plain English sentences. State your answer SUCCINCTLY telling us at a HIGH LEVEL what the code does overall. public static void main(string[] args) double x, y; Scanner in = new Scanner(System.in); x = in.nextdouble(); y = -1.0; while ( x >= 0.0 ) if ( y < x ) y = x; x = in.nextdouble(); System.out.println(y); Page 6

WRITE YOUR ANSWER ON THIS PAGE 15. (8pts) Write a program which reads in values from the user until they enter the sentinel -999. The program should calculate and output the average of all the even numbers entered. The program should also count and output how many of the values entered by the user were negative (not including the sentinel value). Style, conciseness, and appropriate use of programming constructs will be graded in conjunction with correctness. import java.util.scanner; public class UserTest public static void main(string[] args) Scanner in = new Scanner(System.in); Page 7

USEFUL STRING CLASS METHODS (from pages 38-41 in the text) int length() Returns the length of the calling object (which is a string) as a value of type int. int indexof(a_string) Returns the index (position) of the first occurrence of the string A_String in the calling object string. Positions are counted 0,1,2, etc. Returns -1 id a A_String is not found. int indexof(a_string, Start) Returns the index (position) of the first occurrence of the string A_String in the calling object string that occurs at or after position Start. Positions are counted 0,1,2, etc. Returns -1 id a A_String is not found. char charat(position) Returns the character in the calling object strong at the Position. Positions are counted 0,1,2, etc. String substring(start,end) Returns the substring of the calling object string starting from position Start through, but not including, position End of the calling object. Positions are counted 0,1,2, etc.