CS 160 – Summer 16 Exam 1 Prep.

Slides:



Advertisements
Similar presentations
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.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
The switch Statement, DecimalFormat, and Introduction to Looping
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
The Java Programming Language
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
EXAM 1 REVIEW. days until the AP Computer Science test.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Topics for today: 1.Comments 2.Data types 3.Variable declaration.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
(Dreaded) Quiz 2 Next Monday.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Repetition Statements
Information and Computer Sciences University of Hawaii, Manoa
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
CS 160 Final Review.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Loops.
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 5: Control Structures II
User input We’ve seen how to use the standard output buffer
CSS 161: Fundamentals of Computing
SELECTION STATEMENTS (1)
Methods and Parameters
Control Statement Examples
Arrays, For loop While loop Do while loop
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
An Introduction to Java – Part I, language basics
160 Exam 2 Prep.
CS 180 Assignment 6 Arrays.
Expressions and Assignment
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Scope of variables class scopeofvars {
CHAPTER 21 LOOPS 1.
Fundamental Programming
Peer Instruction 4 Control Loops.
CSS161: Fundamentals of Computing
Loops CGS3416 Spring 2019 Lecture 7.
More on iterations using
Presentation transcript:

CS 160 – Summer 16 Exam 1 Prep

Declare and assign a float variable called f to 9. float f = 9F;

Initialize a Scanner called reader that reads from the keyboard. Scanner reader = new Scanner (System.in);

Name the five errors in the following code: public static void main (String [] args){ String s = ”Hello”; int i = s.charAt(‘e’); for (int i = 0; i <= s.length(); i++) if (s.charAt(i) == ‘a’); System.out.println(“cool it’s an a”); if (s == “Hello”) s+= “ World”; } // i does cause a compile error: “error: variable i is already defined in method main(String[])” charAt takes a number and returns a character, what I should have put was s.indexOf(some int) int i is redeclared within the for loop. 3. For loop goes one too far. Should have put i < s.length() or i < s.length() – 1. 4. semicolon after the if statement inside the for loop 5. must check equality of Strings with .equals NOT ==. So instead it should be s.equals(“Hello”);

Read in a double from the Scanner reader and store it into the pre-defined variable d. d = reader.nextDouble();

Write a for loop that prints each character in the predefined variable String s separated by an ampersand (&) all on the same line. for (int i = 0; i < s.length(); i++) System.out.print(s.charAt(i) + “&”);

Close the Scanner called reader. reader.close();

Use the predefined Scanner keys to read and store the following line into the predefined String variable s. Line being read: Hello World! How’s it going? s = keys.nextLine();

Write a switch statement based off of the String variable str Write a switch statement based off of the String variable str. If str equals “Bob” print “Marley”, if str equals “Michael” print “Jackson”, if str equals “Justin” print “Timberlake”, if str is none of those options print str “okay then”. ** NOTE: “miChaeL” and “MICHAEL” should still print ”Jackson”. switch (str.toLowerCase()){ case “bob”: System.out.println(”Marley”); break; case “michael”: System.out.println(“Jackson”); break; case “justin”: System.out.println(“Timberlake”); break; default: System.out.println(“okay then”); }

What does the following code print? char c = '@'; switch (c) { case '@': System.out.println("char c = '@'"); case '$': System.out.println("char c = '$'"); case '4': System.out.println("char c = '4'"); case 'u': System.out.println("char c = 'u'"); case ' ': System.out.println("char c = ' '"); default: System.out.println("char c is not '@', '$', '4', 'u', ' '"); } char c = '@' char c = '$' char c = '4' char c = 'u' char c = ' ' char c is not '@', '$', '4', 'u', ' '

What does the console print based off the following code? public class Practice { public static void main (String [] args){ String s = “Koala Bears”; for (int i = 0; i < s.length(); i+=2) System.out.print(s.charAt(i)); } KaaBas

What does the following code print? for (int l = 0; l > 0; l++) System.out.println(l); nothing

Which one of these would not go to the end of String s Which one of these would not go to the end of String s? (Specify which ones cause errors and which ones are just incorrect (but they compile)? for (int i = 0; i <= s.length(); i++) for (int i = 0; i < s.length()+1; i++) for (int i = 0; i < s.length(); i++) for (int i = 0; i <= s.length() -1; i++) for (int i = 0; i < s.length() -1; i ++) A gives an error (goes one more) B gives an error (goes one more) C is correct D is correct E is incorrect only goes to the second to the last index

Write a do-while loop that will add the sum of all numbers from 0 - 19. int count = 0; int sum = 0; do{ sum += count; count++; }while(count < 20);

Find the 4 errors within the block of code public static void main (String [] args){ long absurdlyLong = 234897523456476345; System.out.println(Integer(15)); System.out.printf(“%s%n”, hola); } public static int double(int num) { for(int i = 0; i <= num; i++) { int counter = 0; counter *= num; return counter; No L following the long value. double is a reserved keyword, I would not be able to name my method double. Counter is declared within the for loop, not a valid return variable. hola has never been declared, if I wanted to print hola, I would need to place quotes around the word, or declare it as a variable somewhere else.