CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdfhttp://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc.

Slides:



Advertisements
Similar presentations
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Advertisements

Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 Java Basics: Data Types, Variables, and Loops “ If debugging is the process of removing software bugs, then programming must be the process of putting.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CS101 Computer Programming I Chapter 4 Extra Examples.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Midterm preview. double int = 2.0; True / FalseThe following is a syntactically correct variable declaration and assignment statement:
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 4 Repetition Statements (loops)
Building Java Programs
Lecture 2: Operations and Data Types
Primitive Data, Variables, Loops (Maybe)
Building Java Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CSS161: Fundamentals of Computing
Topic 4 Expressions and variables
Outline Altering flow of control Boolean expressions
Building Java Programs
Building Java Programs
Topic 4 Expressions and variables
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
C Programming Getting started Variables Basic C operators Conditionals
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Seating “chart” Front - Screen rows Back DOOR.
Building Java Programs
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Building Java Programs
CSS161: Fundamentals of Computing
Building Java Programs
Topic 4 Expressions and variables
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://

CSE 201 – Elementary Computer Programming2 Value of The Variable x   For each of the following program fragments, what is the value of the variable x after the statements execute? (A) int y = 5; int x = y; y = y * 2; (B) int x = 5; x = x / 2; x = x + 3 * x - 3;

CSE 201 – Elementary Computer Programming3 Value of The Variable x   For each of the following program fragments, what is the value of the variable x after the statements execute? (C) double x = 13 / 2; (D) boolean x = ( 2 == 3 );

CSE 201 – Elementary Computer Programming4 Value of expressions   What is the value of each of the following valid expressions? (a) (2 2) (b) (!false) && (!false) (c)( 5 > 2 ) == ( 3 2 ) == ( 3 <= 3 )

CSE 201 – Elementary Computer Programming5 Debug According to Description   Fix the code so that it works according to the supplied documentation (in comments) /* Calculates sum of all of the even * numbers from 0 to limit inclusive. * Assumes limit is initialized above * to a positive integer. */ int i = 0; int sum = 0; while (i != limit) { sum = sum + i; i+=2;}

CSE 201 – Elementary Computer Programming6 Debug According to Description   Solution /* Calculates sum of all of the even * numbers from 0 to limit inclusive. * Assumes limit is initialized above * to a positive integer. */ int i = 0; int sum = 0; while (i <= limit) { sum = sum + i; i+=2;}

CSE 201 – Elementary Computer Programming7 How Many Iterations?   How many times the body of the loop is executed?   0, 1, infinite, or “> 1”?   ”> 1” means more than once but not infinite. int x=1; while (x<10) {System.out.println(x);}

CSE 201 – Elementary Computer Programming8 How Many Iterations?   How many times the body of the loop is executed?   0, 1, infinite, or “> 1”?   ”> 1” means more than once but not infinite. int x=10; while(x<10)System.out.println(x);x=x-1;

CSE 201 – Elementary Computer Programming9 How Many Iterations?   If we put a ; at while loop   How many times x is printed? int x=10; while(x<10);System.out.println(x);x=x-1;

CSE 201 – Elementary Computer Programming10 How Many Iterations?   How many times the body of the loop is executed?   0, 1, infinite, or “> 1”?   ”> 1” means more than once but not infinite. int x=1; while(x!=10){ x = x*2; x = x*2;}

CSE 201 – Elementary Computer Programming11 True/False  In Java I can have both mystring and myString as two distinct variables in same program.  When writing a computer program, everything must be precise and unambiguous.  The starting index position for a string in Java is 0.  When a program written in Java is compiled, it is typically compiled to bytecode

CSE 201 – Elementary Computer Programming12 Multiple Choice 1 In Java, the calculation 3 % 4 produces a result of: a.0 b.1 c.0.75 d.3 e.None of the above.

CSE 201 – Elementary Computer Programming13 Multiple Choice Which of the following represents a character in Java: a.a b.'a' c."a" d.All of the above. e.None of the above.

CSE 201 – Elementary Computer Programming14 Multiple Choice Which of the following is a valid way to create a String object containing the text “Hello there” in Java: a.String tempString = "Hello there"; b.String tempString = "Hello" + " " + "there"; c.String tempString = new String("Hello there"); d.All of the above. e.None of the above.

CSE 201 – Elementary Computer Programming15 Value and Type a."Java" + "is" + "great" b." " c.3 / 2 * 1.0 d."The answer is: " e."The answer is: " + (5 + 5)  For each of the following Java expressions, provide the result of evaluating the expression, and identify the type of this value.

CSE 201 – Elementary Computer Programming16 Value and Type f.'b' > 'a' g * 3 – 4 / 5 h.(((int) 1.0) % * 2) / 2.0 – ((double) 1) i.3 / 6 * (16.3 * / 6.0)  For each of the following Java expressions, provide the result of evaluating the expression, and identify the type of this value.

CSE 201 – Elementary Computer Programming17 Code Tracing int numPeople = 4; System.out.println(numPeople); double bill = ; System.out.println(bill); double tip = bill * 0.2; System.out.println(tip); double total = bill + tip; System.out.println(total); double totalPerPerson = total / numPeople; System.out.println(totalPerPerson);  Trace this code segment and report what this code prints to the screen

CSE 201 – Elementary Computer Programming18 Code Understanding int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); sum = sum + (i*i); i++; i++; }  What does this code do?

CSE 201 – Elementary Computer Programming19 Code Understanding int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); sum = sum + (i*i); i++; i++; }  What does this code do?

CSE 201 – Elementary Computer Programming20 Fix Code Scanner in = new Scanner(System.in); int ans; ans = in.nextInt(); while (ans > 0 && ans 0 && ans < 10){ ans = in.nextInt(); }  Fix code to implement a validation loop to ensure that the user enters a number between 1 and 9 inclusive?

CSE 201 – Elementary Computer Programming21 Code Understanding String foo = "Java!"; String bar = ""; int i = 0; while( i < foo.length()) { bar = foo.charAt(i) + bar; i++; //i.e i=i+1; }System.out.println(bar);  What does this code do?