Introduction to Computer Science Unit 2. Exercises

Similar documents
AP Computer Science A Unit 2. Exercises

AP CS Unit 3: Control Structures Notes

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

Chapter 3. Selections

Practice Midterm 1 Answer Key

CSC 1051 Data Structures and Algorithms I

Algorithms and Conditionals

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AP Computer Science Unit 1. Programs

Selection Statements and operators

Java Coding 3. Over & over again!

Mr. Monroe s Guide to Mastering Java Syntax

AP Computer Science Unit 1. Writing Programs Using BlueJ

Supplementary Test 1

Programming with Java

CS111: PROGRAMMING LANGUAGE II

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Advanced Java Concept Unit 1. Mostly Review

Selection Statements and operators

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS 152 Computer Programming Fundamentals The if-else Statement

Selection and Repetition Revisited

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Unit 4: Classes and Objects Notes

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Introduction to Computer Science Exercises for Unit 4A: Classes and Objects 1. This compiles and runs. What is displayed?

Section 2.2 Your First Program in Java: Printing a Line of Text

Important Java terminology

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Practice Midterm 1. Problem Points Score TOTAL 50

CS111: PROGRAMMING LANGUAGE II

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Full file at

CSC 1051 Data Structures and Algorithms I

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

AP COMPUTER SCIENCE A

M e t h o d s a n d P a r a m e t e r s

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

AP CS Unit 8: Inheritance Exercises

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal

Unit 4: Classes and Objects Exercises

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Fundamentals of Programming Data Types & Methods

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Selection and Repetition Revisited

Section 2.2 Your First Program in Java: Printing a Line of Text

CSC 1051 Data Structures and Algorithms I

Java I/O and Control Structures

Computational Expression

H212 Introduction to Software Systems Honors

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

CS141 Programming Assignment #6

download instant at

Tutorial 8 Date: 15/04/2014

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Task #1 The if Statement, Comparing Strings, and Flags

Bjarne Stroustrup. creator of C++

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

4. Java language basics: Function. Minhaeng Lee

Selection and Repetition

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CIS October 19, 2017

COMP 202 Java in one week

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Java Programming Language. 0 A history

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

d. If a is false and b is false then the output is "ELSE" Answer?

Introduction to Java Programs for Packet #4: Classes and Objects

2.8. Decision Making: Equality and Relational Operators

Java I/O and Control Structures Algorithms in everyday life

1. An operation in which an overall value is computed incrementally, often using a loop.

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Chapter 3 Lab Decision Structures

CS110: PROGRAMMING LANGUAGE I

COMP 202. Java in one week

Welcome to the Primitives and Expressions Lab!

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

int x = 42, y = 33; //1 int z = x + y; //2 System.out.println(x + "+" + y + "=" + z); //3

CONDITIONAL EXECUTION

CSE 114 Computer Science I

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

ECE 161 WEEK 4 Introduction to Programing in Java

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Loops. Repeat after me

AP CS Unit 6: Inheritance Exercises

1 Short Answer (5 Points Each)

Using Java Classes Fall 2018 Margaret Reid-Miller

Introduction to Arrays

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Transcription:

Introduction to Computer Science Unit 2. Exercises Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement. 1. If the user enters 82, what is 2. If the user enters 2, what is Scanner get = new Scanner(System.in); int x = get.nextint(); if ( x < 7 ) System.out.println( "Good" ); System.out.println( "Bad" ); if ( x > 0 ) System.out.println( "Ok" ); 3. If the user enters 10, what is 4. If the user enters 12, what is 5. If the user enters 10, what is 6. If the user enters 4, what is 7. If the user enters 2, what is 8. If x has an initial value of 33, what is its 9. If x has an initial value of 62, what is its Scanner get = new Scanner(System.in); int x = get.nextint(); System.out.println( "AA" ); if ( x <= 11 ) { System.out.println( "BB" ); System.out.println( "CC" ); Scanner get = new Scanner(System.in); int x = get.nextint(); if ( x!= 10 ) x = x + 5; if ( x > 7 ) x = x + 20; x = x + 30; System.out.println( x ); if ( x > 30 && x <= 50 ) { x = x + 10; x = x - 4; if ( x < 40 && x > 60 ) page 1

10. If x has a value of -5 and y has a value of 58, what is 11. If x has a value of 47 and y has a value of 47, what is 12. Select the TRUE statement. a) H is never printed. b) H is always printed. c) H is only printed sometimes. 13. If y has an initial value of 12, what is 14. If y has an initial value of 26, what is 15. If y has an initial value of 7, what is 16. The code to the right does not compile. The is highlighted and the error message is: without if Why? 17. Given that the above error is fixed by the addition of two characters, what will be displayed if the user enters 12? // x and y are declared and initialized if ( x > 30 y >= 60 ) System.out.print( "G" ); if ( x < 100 x > 40 ) System.out.print( "H" ); if ( y < 10 y > 50 ) System.out.print( "K" ); Scanner x = new Scanner( System.in ); System.out.println( "Number?" ); int y = x.nextint(); if ( y > 10 ) y += 10; if ( y > 20 ) y++; System.out.println( y ); Scanner read = new Scanner( System.in ); System.out.println( "Number?" ); int a = read.nextint(); if ( a > 10 && a < 20 ) a = 2 * a; a--; 18. If x has an initial value of -8, what is its 19. If x has an initial value of 9, what is its 20. If x has an initial value of 15, what is its 21. If x has an initial value of 22, what is its 22. If x has a final value of 6, what was its initial if ( x > 10 x < 20 ) if ( x < 10 x > 20 ) x = x + 3; if ( x > 10 && x < 20 ) if ( x < 10 && x > 20 ) x = x + 3; if ( x > 10 && x > 20 ) x = x + 10; page 2

Curly braces identify a block of code. A variable that is declared within a block of code cannot be used outside of that block of code. For example. This code does not compile. The last statement will be highlighted and the compiler error is: cannot find symbol - variable a You won t be tested on this concept but you may run into this problem as you write your programs. Scanner scan = new Scanner( System.in ); System.out.println( "Number?" ); int x = scan.nextint(); if ( x > 10 ) { int a = 4 * x; Block of code. 23. What is int x = 49; double y = Math.sqrt( x ); System.out.println( y ); 24. This code does not compile. The error message is: Type mismatch: cannot convert from double to int What is the problem? int x = 49; int y = Math.sqrt( x ); System.out.println( y ); 25. What is the value of y? int x = -14; int y = Math.abs( x ); 26. List three different import java.util.scanner; numbers that will cause A to be displayed. 27. List three different numbers that will cause B to be displayed. public class Runner{ public static void main( String [] args ){ Scanner x = new Scanner( System.in ); System.out.println( "Enter a positive number" ); double num2 = x.nextdouble(); int num = (int) Math.sqrt( num2 ); if ( num*num == num2 ) System.out.println( "A" ); System.out.println( "B" ); 28. Use the number line to show the possible values for d. double d = 3* Math.random(); 29. Use the number line to show the possible values for d. double d = 5* Math.random() 1; page 3

30. Use the number line to show the possible values for x. int x = (int) ( 2*Math.random() ); 31. Use the number line to show the possible values for x. int x = (int) ( 5 * Math.random() ) - 2; 32. Use the number line to show the possible values for x. int x = (int) ( 4 * Math.random() ) + 2; 33. Complete the line of code so that n is assigned a random integer value within the following limits: int n = 7 n 15 34. Complete the line of code so that n is assigned a random integer value within the following limits: int n = -4 n 8 35. What integers may be printed? int n = (int)( 2 * Math.random()) + 5; if ( Math.random() < 0.5 ) n = -1*n; System.out.print( n ); 36. What integers may be printed? int n = (int)( 4 * Math.random()) + 1; n = 5 * n; System.out.print( n ); 37. If k has an initial value of 13, what is its final 38. If k has an initial value of 22, what is its final 39. If k has an initial value of 4, what is its final // k is declared and assigned a value if ( k < 5 ) k += 2; if ( k < 10 ) k += 5; if ( k < 20 ) k += 6; page 4

40. If k has an initial value of 10, what is its final 41. If k has an initial value of 30, what is its final 42. Lines 7 and 8 can be deleted without changing how the code runs. TRUE FALSE 1 2 3 4 5 6 7 8 // k is declared and assigned a value if ( k < 11 ) k += 4; if ( k < 40 ) k++; if ( k > 11 ) k = k - 2; if ( k > 22 ) k -= 4; 43. If num has a value of 0.2, what is 44. If num has a value of 0.35, what is 45. About what percent of the time will CC appear? 46. If the user enters 38, what is 47. If the user enters -4, what is 48. If the user enters 99, what is 49. If x has an initial value of 8, what is 50. If x has an initial value of 11, what is 51. If x has an initial value of 19, what is double num = Math.random(); if ( num < 0.25 ) { System.out.println( "AA" ); if ( num < 0.5 ) { System.out.println( "BB" ); { System.out.println( "CC" ); Scanner read = new Scanner(System.in); int num = read.nextint(); if ( num < 13) { System.out.println( "ZZ" ); if ( num < 20) { System.out.println( "YY" ); if ( num < 40 ) { System.out.println( "XX" ); System.out.println( "WW" ); if ( x > 10 ) if ( x < 5 ) x++; if ( x <= 20 ) x = 2*x; if ( x > 5 ) x++; System.out.println( x ); page 5

52. What values of x will cause AE to be If AE will never be displayed, write none. 53. What values of x will cause CD to be If CD will never be displayed, write none. 54. What value of x causes only one letter to be And what letter is 55. If x has a value of 56, what is 56. What values of x will cause west to be If west is never displayed, write NEVER. 57. If z has a value of 8, what is 58. If z has a value of 32, what is // x is declared as an int and assigned a value if ( x < 10 ) System.out.print("A"); if ( x > 12 ) System.out.print("B"); System.out.print("C"); if ( x > 10 ) System.out.print("D"); if ( x!= 5 ) System.out.print("E"); // x is declared as an int and assigned a value if ( x >= 20 && x <= 40 ) System.out.println( "north" ); if ( x >= 20 ) System.out.println( "south" ); if ( x < 10 ) System.out.println( "east" ); System.out.println( "west" ); // z is declared as an int and assigned a value if ( z >= 22 ) z = z + 6; if ( z <= 15 ) z = z + 1; if ( z <= 40 ) z = z + 9; z = z + 14; System.out.println( z ); page 6