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.



Similar documents
QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Sample CSE8A midterm Multiple Choice (circle one)

Lecture 2 Notes: Flow of Control

JavaScript: Control Statements I

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

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

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

Conditionals (with solutions)

Unit 6. Loop statements

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

Iteration CHAPTER 6. Topic Summary

6. Control Structures

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

Moving from CS 61A Scheme to CS 61B Java

Introduction to Java

UEE1302 (1102) F10 Introduction to Computers and Programming

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:

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

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

In this Chapter you ll learn:

Example of a Java program

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

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

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Object Oriented Software Design

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Chapter 2 Introduction to Java programming

LOOPS CHAPTER CHAPTER GOALS

Writing Control Structures

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

Lecture 1 Introduction to Java

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

Java Basics: Data Types, Variables, and Loops

Section 6 Spring 2013

1.3 Conditionals and Loops

While Loop. 6. Iteration

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

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

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

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Homework/Program #5 Solutions

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

Introduction to Object-Oriented Programming

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

1 Description of The Simpletron

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

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

Java Interview Questions and Answers

Chapter 2: Elements of Java

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

Boolean Expressions & the if Statement

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

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

Two-way selection. Branching and Looping

Basic Java Syntax. Program Structure

Object Oriented Software Design

Inside the Java Virtual Machine

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

CS170 Lab 11 Abstract Data Types & Objects

Using Files as Input/Output in Java 5.0 Applications

Moving from C++ to VBA

Building Java Programs

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

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

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

Java Program Coding Standards Programming for Information Technology

Conditional Statements Summer 2010 Margaret Reid-Miller

aicas Technology Multi Core und Echtzeit Böse Überraschungen vermeiden Dr. Fridtjof Siebert CTO, aicas OOP 2011, 25 th January 2011

Java How to Program, 5/e Test Item File 1 of 5

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

Statements and Control Flow

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

PHP Tutorial From beginner to master

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Statements and Control Flow

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

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Chapter One Introduction to Programming

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Selection Statements

C# programming. Introduction. By Per Laursen

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

The While Loop. Objectives. Textbook. WHILE Loops

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

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

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Topic 11 Scanner object, conditional execution

Using Two-Dimensional Arrays

Building Java Programs

JAVA ARRAY EXAMPLE PDF

Chapter 5. Selection 5-1

Informatica e Sistemi in Tempo Reale

Arrays. Introduction. Chapter 7

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

LAB4 Making Classes and Objects

Building Java Programs

public static void main(string[] args) { System.out.println("hello, world"); } }

Transcription:

Java How to Program, 5/e Test Item File 1 of 5 Chapter 5 Section 5.2 5.2 Q1 Counter-controlled repetition requires a.a control variable and initial value. b.a control variable increment (or decrement). c.a condition that tests for the final value of the control variable. d.all of the above. ANS: d. All of the above. 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. Section 5.3 5.3 Q1: Consider the following two Java code segments: Segment 1 Segment 2 int i = 0; for ( int i = 0; i <= 20; i++ ) { while ( i < 20 ) { System.out.println( i ); i++; System.out.println( i ); Which of the following statements are true? a.the output from these segments is not the same. b.the scope of the control variable i is different for the two segments. c.both (a) and (b) are true. d.neither (a) nor (b) is true. ANS: c. Both (a) and (b) are true. 5.3 Q2: Consider the classes below: public class TestA { public static void main( String args[] ) { int x = 2, y = 20, counter = 0; for ( int j = y % x; j < 100; j += ( y / x ) ) { counter++; public class TestB { public static void main(string args[]) { int counter = 0; for ( int j = 10; j > 0; --j ) { ++counter;

Java How to Program, 5/e Test Item File 2 of 5 Which of the following statements is true? a.the value of counter will be the different at the end of each for loop for each class. b.the value of j will be the same for each loop for all iterations c.both (a) and (b) are true. d.neither (a) nor (b) is true. ANS: d. Neither (a) nor (b) is true. Section 5.4 5.4 Q1: Which of the following for-loop control headers results in equivalent numbers of iterations: A.for ( int q = 1; q <= 100; q++ ) B.for ( int q = 100; q >= 0; q-- ) C.for ( int q = 99; q > 0; q -= 9 ) D.for ( int q = 990; q > 0; q -= 90 ) a.a and B. b.c and D. c.a and B have equivalent iterations and C and D have equivalent iterations. d.none of the loops have equivalent iterations. ANS: b. C and D. 5.4 Q2: Which of the following will count down from 10 to 1 correctly? a.for ( int j = 10; j <= 1; j++ ) b.for ( int j = 1; j <= 10; j++ ) c.for ( int j = 10; j > 1; j-- ) d.for ( int j = 10; j >= 1; j-- ) ANS: d. for ( int j = 10; j >= 1; j-- ) Section 5.5 5.5 Q1: Which of the following statements about a do while repetition statement is true? a.the body of a do while loop is executed only if the terminating condition is true. b.the body of a do while loop is executed only once. c.the body of a do while loop is always executed at least once. d.none of the above ANS: c. The body of a do while loop is always executed at least once. 5.5 Q2: Which of the following will not help prevent infinite loops? a.include braces around the statements in a do while statement. b.ensure that the header of a for or while statement is not followed by a semicolon. c.if the loop is counter-controlled, the body of the loop should increment or decrement the counter as needed. d.if the loop is sentinel-controlled, ensure that the sentinel value is input eventually. ANS: a. Include braces around the statements in a do while statement. Section 5.6 5.6 Q1: For the two code segments below: Segment A int q = 5; System.out.println( 1 ); System.out.println( 2 ); System.out.println( 3 );

Java How to Program, 5/e Test Item File 3 of 5 System.out.println( 4 ); System.out.println( 5 ); : System.out.println( "" ); Segment B q = 4; System.out.println( 1 ); System.out.println( 2 ); System.out.println( 3 ); System.out.println( 4 ); System.out.println( 5 ); : System.out.println( "" ); Which of the following statements is true? b.the output for Segment A is: c.the output for Segment B is: 4 d.the output for Segment B is: 45 e.the output for Segment A is: 5 ANS: d. The output for Segment A is: 5 5.6 Q2: For the code segment below, System.out.println( "apple" ); System.out.println( "orange" ); System.out.println( "banana" ); System.out.println( "pear" ); System.out.println( "grapes" ); : System.out.println( "kiwi" );

Java How to Program, 5/e Test Item File 4 of 5 Which of the following values for q will result in kiwi being output? a.2. b.4 and anything greater than 4. c.1. d.3. ANS: b. 4 and anything greater than 4. Section 5.7 5.7 Q1: Which of the following statements about break and continue statements is true? a.the continue statement is used to exit a repetition structure early and continue execution after the loop. b.the continue statement is used to continue after a switch statement. c.the break statement, when executed in a while, for or do while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. d.the continue statement, when executed in a while, for or do while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. ANS: d. The continue statement, when executed in a while, for or do while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. 5.7 Q2: To exit out of a loop completely, and resume the flow of control at the next line in the method, use. a.a continue statement. b.a break statement. c.a return statement. d.any of the above. ANS: b. A break statement. Section 5.8 5.8 Q1: Which of the following statements is not true? A.A break statement can only break out of an immediately enclosing while, for, do while or switch statement. B.Labeled break statements break out of any number of enclosing repetition structures. C.A continue statement proceeds with the next iteration of the immediately enclosing while, for, do while statement. D.Labeled continue statements break out of any number of enclosing repetition structures, and continue execution with next iteration of the labeled repetition structure. a.a and C. b.b and D. c.none of the above are true. d.all of the above are true. ANS: d. All of the above are true. 5.8 Q2: Labeled break statements cannot be used to break out of which of the following? a.a while statement. b.a method. c.a for loop. d.a switch statement. ANS: b. A method. Section 5.9 5.9 Q1: Consider the code segment below. if ( gender == 1 ) if ( age >= 65 )

Java How to Program, 5/e Test Item File 5 of 5 This segment is equivalent to which of the following? a. if ( gender == 1 age >= 65 ) b. if ( gender == 1 && age >= 65 ) c. if ( gender == 1 AND age >= 65 ) d. if ( gender == 1 OR age >= 65 ) ANS: b. if ( gender == 1 && age >= 65 ) 5.9 Q2: Which case of the following would warrant using the boolean logical inclusive OR ( ) rather than the conditional OR ( )? a.testing if two conditions are both true. b.testing if at least one of two conditions is true. c.testing if at least one of two conditions is true when the right operand has a required side effect. d.testing if at least one of two conditions is true when the left operand has a required side effect. ANS: c. Testing if at least one of two conditions is true when the right operand has a required side effect. Section 5.10 5.10 Q1: Which statement below is not true? a.structured programming produces programs are easier to test. b.structured programming requires four forms of control. c.structured programming produces programs that are easier to modify a.structured programming promotes simplicity. ANS: b. Structured programming requires four forms of control. (Only three forms are necessary: sequence, selection, repetition) 5.10 Q2: Which of the following is NOT a way that repetition is implemented in Java programs? a.while structures. b.do while structures. c.for structures. d.if structures. ANS: d. if structures.