Conditionals (with solutions)



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

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

Variables, Constants, and Data Types

Chapter 2: Elements of Java

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

CS 241 Data Organization Coding Standards

Chapter 5. Selection 5-1

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

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

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

Iteration CHAPTER 6. Topic Summary

Moving from CS 61A Scheme to CS 61B Java

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

Introduction to Java Lecture Notes. Ryan Dougherty

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.

The if Statement and Practice Problems

Informatica e Sistemi in Tempo Reale

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

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

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

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

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?

Stacks. Linear data structures

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

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

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

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

Pseudo code Tutorial and Exercises Teacher s Version

Pseudo Code Practice Problems:

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

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

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

Topic 11 Scanner object, conditional execution

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

While Loop. 6. Iteration

Unit 6. Loop statements

LOOPS CHAPTER CHAPTER GOALS

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

Building Java Programs

Some Scanner Class Methods

Chapter 8 Selection 8-1

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

Number Representation

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

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

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Conditional Statements Summer 2010 Margaret Reid-Miller

TETRIX Add-On Extensions. Encoder Programming Guide (ROBOTC )

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

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

JavaScript: Control Statements I

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

Introduction to Java. CS 3: Computer Programming in Java

>

GCE. Computing. Mark Scheme for January Advanced Subsidiary GCE Unit F452: Programming Techniques and Logical Methods

Building Java Programs

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

Chapter 3 Operators and Control Flow

Abstraction and Abstract Data Types

Example of a Java program

In this Chapter you ll learn:

Ad Hoc Advanced Table of Contents

Visual Logic Instructions and Assignments

Selection Statements

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

Adding and Subtracting Positive and Negative Numbers

Lecture 2 Notes: Flow of Control

Building Java Programs

Python Lists and Loops

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

Sample CSE8A midterm Multiple Choice (circle one)

Conditions & Boolean Expressions

Boolean Expressions & the if Statement

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Lab Manual. Databases. Microsoft Access. Peeking into Computer Science Access Lab manual

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

Introduction to SQL for Data Scientists

Introduction to Python

Chapter 1: Computer Systems

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

13 Classes & Objects with Constructors/Destructors

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Searching Guide Version 8.0 December 11, 2013

VB.NET Programming Fundamentals

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

Program Logic to Java GEEN163

Java Interview Questions and Answers

COMPUTER SCIENCE. Paper 1 (THEORY)

General Software Development Standards and Guidelines Version 3.5

Building Java Programs

Section IV.1: Recursive Algorithms and Recursion Trees

C++ program structure Variable Declarations

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

Introduction to Java

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Statements and Control Flow

Software development and programming. Software

So far we have considered only numeric processing, i.e. processing of numeric data represented

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

Transcription:

Conditionals (with solutions) For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations: final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87; 1. if (num1 < MAX) apple 2. if (num2 <= MAX) apple orange 3. if (MAX > num3) orange 4. if (num3 >= LIMIT) orange pear poor indentation 5. if (num2 == MAX) apple orange pear 6. if (num3-num2 > 2*MAX) apple 7. if (LIMIT+num3 <= 150) pear

8. if (2*num1!= num2) apple 9. if (LIMIT%num1 + 4 == num1 + (MAX-num2)) pear banana System.out.println ("banana"); 10. if (num1 < MAX) apple if (LIMIT >= num2) orange 11. if (LIMIT <= LIMIT) orange if (num3 == num1) 12. if (num2 > 18) orange if (num1 < 0) pear 13. if (LIMIT >= 4*num2) apple if (MAX == 25)

14. if (num2 < num1) pear if (num3 < LIMIT) poor indentation 15. if (num3 == 87) pear if (num2!= MAX) 16. if (num1+num2 > num3) orange if (num2*limit!= 3298) 17. if (LIMIT%MAX == 3) orange if (num2 == MAX) 18. if (num3 >= MAX) apple orange if (MAX/num2 == 1) banana kiwi coconut if (LIMIT-num3 > num1+2) System.out.println ("banana"); System.out.println ("kiwi"); if (num2*2 == MAX*2) System.out.println ("grapefruit");

System.out.println ("lime"); System.out.println ("coconut"); 19. if (num2 > num1 && LIMIT!= 100) orange 20. if (num3 == num2 && MAX > 50) orange 21. if (num1 > 7 && LIMIT <= 100) apple orange 22. if (num3 < 40 num3 > 50) apple orange 23. if (MAX == LIMIT num1*2 == num2) orange 24. if (num2%2!= 0 num3 > LIMIT) apple orange 25. if (MAX == 25 && num2!= MAX num1 < num3) apple orange 26. if (num3 == 87 num2 > num1 && MAX > LIMIT) apple orange 27. if ((num3 == 87 num2 > num1) && MAX > LIMIT)orange

For exercises 28 to 41, write code segments that will perform the specified action. Assume that all variables have already been declared and given values. 28. Print "Hurrah!" if sum is evenly divisible by count. if (sum%count == 0) System.out.println ("Hurrah!"); 29. Increment the integer variable total if total is zero and decrement total otherwise. if (total == 0) total++; total--; 30. Print "num is zero", "num is negative", or "num is positive" as appropriate based on the current value of num. if (num == 0) System.out.println ("num is zero"); if (num < 0) System.out.println ("num is negative"); System.out.println ("num is positive"); 31. Print "num is zero", "num is even", or "num is odd" as appropriate based on the current value of num. if (num == 0) System.out.println ("num is zero"); if (num%2 == 0) System.out.println ("num is even"); System.out.println ("num is odd"); 32. Print "Victory" only if result is greater than or equal to 500 and penalty is equal to zero (use nested ifs). if (result >= 500) if (penalty == 0) System.out.println ("Victory");

33. Print "Victory" only if result is greater than or equal to 500 and penalty is equal to zero (use logical operators). if (result >= 500 && penalty == 0) System.out.println ("Victory"); 34. Assign the smallest of two integer values num1 and num2 to the variable smallest. (use an if- statement) if (num1 < num2) smallest = num1; smallest = num2; 35. Assign the smallest of two integer values num1 and num2 to the variable smallest. (use the conditional operator) smallest = (num1 < num2)? num1 : num2; 36. Assign the smallest of three integer values num1, num2, and num3 to the variable smallest. (do not use logical operators) if (num1 < num2) if (num1 < num3) smallest = num1; smallest = num3; if (num2 < num3) smallest = num2; smallest = num3; 37. Assign the smallest of three integer values num1, num2, and num3 to the variable smallest. (use logical operators) if (num1 < num2 && num1 < num3) smallest = num1; if (num2 < num3) smallest = num2; smallest = num3;

38. Print "This is a vowel." if the character stored in the variable letter is a lowercase vowel. if (letter == 'a' letter == 'e' letter == 'i' letter == 'o' letter == 'u') System.out.println ("This is a vowel."); 39. Of the two characters stored in the variables ch1 and ch2, print the one which comes later in the Unicode character set. if (ch1 < ch2) System.out.println (ch1); System.out.println (ch2); 40. Print "Uppercase", "Lowercase", or "Not a letter" depending on whether the character stored in ch is an uppercase alphabetic character, a lowercase alphabetic character, or not an alphabetic character at all. if (ch >= 'A' && ch <= 'Z') System.out.println ("Uppercase"); if (ch >= 'a' && ch <= 'z') System.out.println ("Lowercase"); System.out.println ("Not a letter"); 41. Print "Equal" if two floating point values stored in val1 and val2 are exactly equal, "Essentially Equal" if they are within 0.0001 of each other, or "Not Equal" otherwise. if (val1 == val2) System.out.println ("Equal"); if (Math.abs(val1-val2) < 0.0001) System.out.println ("Essentially Equal"); System.out.println ("Not Equal");