Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) 12345 e) DEFAULT_VALUE f) bad-choice.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Control Structures.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 4 - Control Statements
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Pemrograman Dasar Control Flow Statements: Decision Making or Selection PTIIK - UB 1.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Air Force Institute of Technology Electrical and Computer Engineering
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
CS110 Programming Language I
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Logic & program control part 3: Compound selection structures.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
PHP - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
In Java.... Variables contain the values of primitive data types Value Types.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
int num = 22; if (num > 0) if (num % 5 == 0) System.out.println(num); else System.out.println(num + “ is negative”);
A: A: double “4” A: “34” 4.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Expressions Methods if else Statements Loops Potpourri.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Java String Methods - Codehs
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Relational Operator and Operations
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4 Repetition Statements (loops)
2. Java language basics (2)
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Review Operation Bingo
null, true, and false are also reserved.
An Introduction to Java – Part I, language basics
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Control structures Chapter 3.
Feedback Pace Different ideas for delivery Debugging strategies
Control structures Chapter 3.
The Java switch Statement
Module 2 - Part 1 Variables, Assignment, and Data Types
CS2011 Introduction to Programming I Selections (I)
Nate Brunelle Today: Conditional Decision Statements
Control structures Chapter 3.
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Discussion1 Quiz

Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice g) 1two3 h) first name i) last.name

A1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice g) 1two3 h) first name i) last.name

Q2 Which of these identifiers violate the naming convention for object names? a) R2D2 b) isthisokay? c) Java d) 3CPO e) ThisIsReallyOkay f) Java g) anotherbadone

A2 Which of these identifiers violate the naming convention for object names? a) R2D2 b) isthisokay? c) Java d) 3CPO e) ThisIsReallyOkay f) Java g) anotherbadone

Q3 What is the output of the following statements a) System.out.println( (a++)++ ); b) System.out.println( 2.5 == 5 / 2 ); c) System.out.println( /2 ); d) System.out.println( 5 % 3); e) System.out.println(5 + 4/2.0); f) float f = 1.1; System.out.println(f – 0.1); g) int a = 10; System.out.println(a = 11);

A3 What is the output of the following statements a) System.out.println( (a++)++ ); //error! b) System.out.println( 2.5 == 5 / 2 ); //false c) System.out.println( /2 ); //3.0 d) System.out.println( 5 % 3); //2 e) System.out.println(5 + 4/2.0); // 7.0 f) float f = 1.1; System.out.println(f – 0.1); //error! g) int a = 10; System.out.println(a = 11); //11

Q4 What is the result? boolean b1 = true; Boolean b2 = false; System.out.println(b1 || b2 = true); System.out.println(b1); System.out.println(b2);

A4 What is the result? boolean b1 = true; Boolean b2 = false; System.out.println(b1 || b2 = true); //b1 || b2 is evaluated first before = System.out.println(b1); System.out.println(b2);

Q5 What is the result? boolean b1 = true; Boolean b2 = false; System.out.println(b1 || (b2 = true)); System.out.println(b1); System.out.println(b2);

A5 What is the result? boolean b1 = true; Boolean b2 = false; System.out.println(b1 || (b2 = true)); //true System.out.println(b1); //true System.out.println(b2); //false

Q6 What is the result? boolean b1 = true; Boolean b2 = false; System.out.println(b1 && (b2 = true)); System.out.println(b1); System.out.println(b2);

A6 What is the result? boolean b1 = true; Boolean b2 = false; System.out.println(b1 && (b2 = true)); //true System.out.println(b1); //true System.out.println(b2); //true

Q7 What’s the output? String s1 = new String("John"); String s2 = new String("John"); if (s1 == s2) System.out.println("s1 == s2 is true"); else System.out.println("s1 == s2 is false");

A7 What’s the output? String s1 = new String("John"); String s2 = new String("John"); if (s1 == s2) System.out.println("s1 == s2 is true"); else System.out.println("s1 == s2 is false"); // s1 == s2 is false

Q8 Is there any problem with this? int a = 10; double b = 1.0; a = a + b; System.out.println(a);

A8 Is there any problem with this? int a = 10; double b = 1.0; a = a + b; //type error System.out.println(a);

Q8 Is there any problem with this? int a = 10; double b = 1.0; a += b; System.out.println(a);

A8 Is there any problem with this? int a = 10; double b = 1.0; a += b; System.out.println(a); //no problem! lhs op= rhs is actually lhs op= (type of lhs)rhs