if (third > second) if (first < third / (double) second) if (s1.compareto(s2) > 0) else switch (third % 3) { case 3: break; case 2:

Similar documents
Basics of Java Programming Input and the Scanner class

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

Introduction to Java

Introduction to Java. CS 3: Computer Programming in Java

Sample CSE8A midterm Multiple Choice (circle one)

Introduction to Programming

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

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

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

1. Writing Simple Classes:

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

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

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

AP Computer Science Static Methods, Strings, User Input

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

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

CS 121 Intro to Programming:Java - Lecture 11 Announcements

In this Chapter you ll learn:

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

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

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

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

Lab 5: Bank Account. Defining objects & classes

Using Files as Input/Output in Java 5.0 Applications

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

Topic 11 Scanner object, conditional execution

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

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

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

java Features Version April 19, 2013 by Thorsten Kracht

Programming Fundamentals I CS 110, Central Washington University. November 2015

CS 106 Introduction to Computer Science I

Homework/Program #5 Solutions

Building Java Programs

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

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

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

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

Interactive Programs and Graphics in Java

Arrays in Java. Working with Arrays

CSE 8B Midterm Fall 2015

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

AP Computer Science Java Subset

Chapter 2: Elements of Java

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

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

Chapter 2 Introduction to Java programming

Programming Languages CIS 443

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

Iteration CHAPTER 6. Topic Summary

Java Basics: Data Types, Variables, and Loops

Reading Input From A File

(Eng. Hayam Reda Seireg) Sheet Java

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

An Incomplete C++ Primer. University of Wyoming MA 5310

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

Section 6 Spring 2013

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Java Interview Questions and Answers

System.out.println("The sum of the two fractions is " + sumnumerator + "/" + sumdenominator + ". In decimal: " + sumdecimal);

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

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

Building Java Programs

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

Statements and Control Flow

Some Scanner Class Methods

Lecture 5: Java Fundamentals III

CS 106 Introduction to Computer Science I

Lecture 1 Introduction to Java

Chapter 3. Input and output. 3.1 The System class

Object-Oriented Programming in Java

Stacks. Linear data structures

Unit 6. Loop statements

Basic Java Syntax. Program Structure

JAVA ARRAY EXAMPLE PDF

CS170 Lab 11 Abstract Data Types & Objects

Course Intro Instructor Intro Java Intro, Continued

objectives chapter Define classes that act like blueprints for new objects, made of variables and methods. Explain encapsulation and Java modifiers.

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

13 File Output and Input

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

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

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

Programmierpraktikum

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

LOOPS CHAPTER CHAPTER GOALS

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:

Introduction to Object-Oriented 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

Number Representation

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.

Interactive Applications (CLI) and Math

Install Java Development Kit (JDK) 1.8

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Primitive data types in Java

Comp 248 Introduction to Programming

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

Transcription:

Name CS/IT114: Exam #2, 12/10/2015 (100 points total) *** PLEASE TURN OFF ALL CELLPHONES *** You may use your crib sheet only (one HANDWRITTEN 8-1/2 x 11 page both sides). You will have until the end of test period, unless you get time-and-a-half. Spend only about one minute per point on each question to complete the exam on time. 1. Expression Evaluation (20 Points) See corresponding sections of Exam 1, Practice Exam 1, and part 4 on the CS110 practice exam (plus solutions): http://www.cs.umb.edu/~bobw/cs110/exam1.pract.pdf 2. Selection Statements (20 points): What is printed by the following code segment? double first = 0.0; int second = 46; int third = 23; String s1 = "hello", s2 = "World"; if (third > second) System.out.println("a"); if (first < third / (double) second) System.out.println("b"); if (s1.compareto(s2) > 0) System.out.println("c"); else System.out.println("d"); System.out.println("e"); System.out.println("f"); switch (third % 3) { case 3: System.out.println("g"); break; case 2: System.out.println("h"); break; case 1: System.out.println("i"); default: System.out.println("j"); break; // Printed? (Yes or No) // a. // b. // c. // d. // e. // f. // g. // h. // i. // j.

3. Loops (20 points): Study the following code segment. int j = 5; while (j >= 0) { for (int i = 0; i <= j; i++) System.out.print('-'); System.out.println((char) ('z' - j) + "."); j--; a. What is printed? b. How would you alter the code to make the letters print at the start of each line? (Otherwise, output should look the same.) See also part 6a + solution on the CS110 practice exam: http://www.cs.umb.edu/~bobw/cs110/exam1.pract.pdf

4. Arrays (20 points) // Part a. int[] numbers = { 0, 2, 1, 3, 2, 1 ; char[] chars = new char[numbers.length]; for (int i = 0; i < chars.length; i++) chars[i] = (char)('q' + numbers[i]); // consider writing out contents of chars below //What prints? System.out.println((int) Math.pow(chars.length, 2)); // System.out.println(chars[3] + ": " + numbers[3]); // System.out.println(chars[5] - 'a'); // System.out.println("hello" + chars[1]); // System.out.println(("" + chars[5]).touppercase()); // // Part b. String str = "Hello, how are you today?"; // Declare a char array called "chars2" and set it equal to the // char array of String s. (You can call "tochararray()" using str) // Using a loop (of your choice) print every third character // of chars2. (HINT: If x is some integer >= zero, then x % 3 // will always be either 0, 1, or 2.) // Reverse the order of the elements in chars2. (HINT: The // variable str still has the original String, and the last // position in it is the length minus one.) See also part 3a + solution on the second CS110 practice exam: http://www.cs.umb.edu/~bobw/cs110/exam2.pract.pdf

5. Program Writing (20 Points) Finish the "main" method that will create two String arrays to hold authors and titles of the books (where the size of each array is the number of books), execute a loop that will prompt the user for the book author (and store it in the authors array) and for the book title (and store it in the titles array), and run a second loop to print each book. Sample output (will be different each time depending on user input): How many books? 3 Book #1 Author? Stephen King Title? The Stand Book #2 Author? R. L. Stine Title? Goosebumps The underlined portion represents what the user has typed in, in response to the program's prompts. Book #3 Author? JRR Tolkien Title? The Hobbit Your books (3 total): The Stand, by Stephen King Goosebumps, by R. L. Stine The Hobbit, by JRR Tolkien Fill in the missing parts inside of main: import java.util.scanner; public class Books { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int numbooks; System.out.print("How many books?"); numbooks = scan.nextint(); scan.nextline();

Answers: 1. Expression Evaluation: See resources indicated. 2. Selection Statements a. No b. Yes c. Yes d. No e. Yes f. Yes g. No h. Yes i. No j. No 3. Loops a. ------u. -----v. ----w. ---x. --y. -z. b. int j = 5; while (j >= 0) { System.out.print((char) ('z' - j)); for (int i = 0; i <= j; i++) System.out.print('-'); System.out.println("."); j--; Result: u------. v-----. w----. x---. y--. z-. 4. Arrays A. 36 t: 3 17 hellos R B. String str = "Hello, how are you today?"; char[] chars2 = str.tochararray(); for (int i = 0; i < chars2.length; i++) if (i % 3 == 2) System.out.println(chars2[i]); for (int i = 0; i < chars2.length; i++) chars2[i] = str.charat(str.length() - 1 - i);

5. Writing Programs import java.util.scanner; public class Books { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int numbooks; System.out.print("How many books?"); numbooks = scan.nextint(); scan.nextline(); String[] authors = new String[numBooks]; String[] titles = new String[numBooks]; System.out.println(); for (int i = 0; i < numbooks; i++) { System.out.println("Book #" + (i+1)); System.out.print("Author? "); authors[i] = scan.nextline(); System.out.print("Title? "); titles[i] = scan.nextline(); System.out.println(); System.out.println("Your books (" + numbooks + " total)"); for (int i = 0; i < numbooks; i++) System.out.println(titles[i] + ", by " + authors[i]);