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



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

Sample CSE8A midterm Multiple Choice (circle one)

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

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

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

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

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

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

Introduction to Java

Conditionals (with solutions)

CS170 Lab 11 Abstract Data Types & Objects

Programming Languages CIS 443

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

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

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

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

LOOPS CHAPTER CHAPTER GOALS

Statements and Control Flow

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

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

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

Chapter 2: Elements of Java

AP Computer Science Static Methods, Strings, User Input

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

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

Pemrograman Dasar. Basic Elements Of Java

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

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Chapter 2 Introduction to Java programming

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

Example of a Java program

Introduction to Java Lecture Notes. Ryan Dougherty

Java Basics: Data Types, Variables, and Loops

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

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

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

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

Moving from CS 61A Scheme to CS 61B Java

Chapter 3. Input and output. 3.1 The System class

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

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Contents. 9-1 Copyright (c) N. Afshartous

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

Introduction to Java. CS 3: Computer Programming in Java

Topic 11 Scanner object, conditional execution

Section 6 Spring 2013

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

AP Computer Science Java Subset

Using Files as Input/Output in Java 5.0 Applications

Building Java Programs

CS 106 Introduction to Computer Science I

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

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

Variables, Constants, and Data Types

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

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

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

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

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

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

CS106A, Stanford Handout #38. Strings and Chars

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

Iteration CHAPTER 6. Topic Summary

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

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

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Building Java Programs

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Install Java Development Kit (JDK) 1.8

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Introduction to Programming

Unit 6. Loop statements

Java Programming Fundamentals

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

Lecture 5: Java Fundamentals III

3 length + 23 size = 2

Informatica e Sistemi in Tempo Reale

Compiler Construction

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

CmpSci 187: Programming with Data Structures Spring 2015

Java Crash Course Part I

JAVA ARRAY EXAMPLE PDF

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

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

LAB4 Making Classes and Objects

Java CPD (I) Frans Coenen Department of Computer Science

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Software Development. Chapter 7. Outline The Waterfall Model RISKS. Java By Abstraction Chapter 7

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.

Java Interview Questions and Answers

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

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

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

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

Stacks. Linear data structures

D06 PROGRAMMING with JAVA

Chapter 2 Elementary Programming

Transcription:

Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points if correct on the first try, but only 1 point if right on the second try. Each multiple-choice question has only one correct answer (we think). 1 st 2 nd 1. The program fragment int n, sum = 0, count = 0; do n = in.nextint(); sum += n; count++ ; while (n!= -1); System.out.println( sum / count ); is applied to the keyboard input 3 5-1 What will be the output? A. 4 B. 4.0 C. 2.3 D. 3 E. 2 Summer, 2006 Dr. H. H. Leitner

Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 2. Which one of the following is a syntactically valid boolean expression whose value is true? A. ((7 < 3)! False) B. ((5 < 4) && (3 * 3 > 8)) C. ((3 < 5) = false) D. (5 >= 5 > 3) E. (1!= 2 5 == 6) 3. Assuming variable n has been properly declared as an int, the statement switch (n) case 1: System.out.println("The number is 1" ); case 2: System.out.println("The number is 2" ); break; default: System.out.println("The number is not 1 or 2"); A. Contains a syntax error, and therefore won t compile. B. Can never print the message "The number is 2" C. Is equivalent to: if (n==1) System.out.println( "The number is 1" ); else if (n==2) System.out.println( "The number is 2" ); else System.out.println("The number is not 1 or 2"); D. Will always print "The number is 2" when n equals 1 E. Will cause an execution error whenever n has a value less than 1. Summer, 2006 Dr. H. H. Leitner

Computer Science S-111a: Intensive Introduction to Computer Science Using Java Page 3 4. The expression (int) (Math.random() * 9-3) produces integer values in the range A. 0 to 6, inclusive B. 0 to 5, inclusive C. 3 to 11, inclusive D. -3 to 5, inclusive E. none of the above 5. Suppose the following code fragment is executed, String s = in.nextline(); char ch = s.charat(s.length() 3); and the input that the user types on the keyboard in response to this code consists of S-111a ROCKS The character variable ch now contains: A. a newline character B. 'S' C. 'K' D. 'C' E. 'O'

Computer Science S-111a: Intensive Introduction to Computer Science Using Java Page 4 6. The following complete program is designed to compute the number of decimal digits, ndigits, in an integer x (where x 0). class Prob6 public static void main (String [] args) int power = 1, ndigits, x; System.out.print("Input a value greater than 0: "); x = in.nextint(); while (x > power) power = * 10; ndigits ++; System.out.println("Number of digits is " + ndigits ); Find and correct three small, distinct errors in this code so that it will work as intended, even if x is a power of 10. There might be more than 3 errors, but you need to fix just 3 of them. 7. Consider the creation of a Time class, the values of which represent a time object containing an hour and a minute within a single day. In this problem you will work with the following methods: a constructor method for creating Time objects; an addminutes method to increment a Time object, returning a new Time object a tostring method to output a Time object; and a priorto method to compare two Time objects (to determine which is earlier ) For example, a method that uses this class might contain the following: Time dawn = new Time(6, 35); // creates the AM value 6:35 Time dusk = dawn.addminutes(60*12); // creates the PM value 18:35 Time m = new Time (0, 0); // creates the midnight value System.out.println ( dawn ); System.out.println ( dusk ); System.out.println ( m ); if (m.priorto(dusk)) System.out.println("Hello");

Computer Science S-111a: Intensive Introduction to Computer Science Using Java Page 5 This code will print 6:35 AM 6:35 PM Midnight Hello Your task is to replace each of the five??????????s with correct Java code. Note that tostring() should return time values in an intelligent fashion. The dusk value was not output as 18:35, but (correctly) instead as 6:35 PM. Similarly, a Time value whose number of minutes is less than 10 should contain a zero in the output (e.g., 11:05 AM would be printed, and not 11:5 AM). class Time private int hour, minute; public Time (int h, int m)??????????1 public Time addminutes (int m) int totalminutes = (60*hour + minute + m) % (24*60); if (totalminutes < 0) totalminutes = totalminutes + 24*60; return??????????2 public String tostring () if ((hour == 0) && (minute == 0)) return "midnight"; else if (??????????3 ) return "noon"; else??????????4 public boolean priorto (Time t)??????????5