CS 163/164 Exam 2 Review

Similar documents
CS 163/164 Exam 2 Review

CS163/164 Final Exam Study Session

Computational Expression

Programming with Java

PROGRAMMING FUNDAMENTALS

Programming II (CS300)

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Programming II (CS300)

Practice Midterm 1. Problem Points Score TOTAL 50

Problem Grade Total

MIDTERM REVIEW. midterminformation.htm

Final Exam Practice. Partial credit will be awarded.

CSCI 355 Lab #2 Spring 2007

CS 101 Exam 1 Spring 200 Id Name

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Program Fundamentals

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

C# MOCK TEST C# MOCK TEST I

Introduction to Programming Using Java (98-388)

Computer Programming, I. Laboratory Manual. Final Exam Solution

Introduction to Classes and Objects

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Integer Data Types. Data Type. Data Types. int, short int, long int

AP Computer Science A

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Full file at

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Homework #3 CS2255 Fall 2012

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

Coding Standards for Java

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

5) (4 points) What is the value of the boolean variable equals after the following statement?

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Chapter 2: Basic Elements of Java

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Question: Total Points: Score:

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Array. Prepared By - Rifat Shahriyar

CS18000: Problem Solving And Object-Oriented Programming

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Software Practice 1 Basic Grammar

Objectives. In this chapter, you will:

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CSCI 355 LAB #2 Spring 2004

Exam 3 Chapters 7 & 9

CS 1331 Exam 1 ANSWER KEY

Chapter 3 Lab Decision Structures

Peer Instruction 1. Elementary Programming

Fundamentals of Programming Data Types & Methods

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

Introduction to Classes and Objects

Class Foo. instance variables. instance methods. Class FooTester. main {

CS 2340 Objects and Design - Scala

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Chapter 2: Data and Expressions

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS110: PROGRAMMING LANGUAGE I

2.8. Decision Making: Equality and Relational Operators

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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

AP CS Unit 3: Control Structures Notes

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

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

CS 101 Fall 2005 Midterm 2 Name: ID:

Question: Total Points: Score:

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

I. True/False: (2 points each)

Full file at

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

CS242 COMPUTER PROGRAMMING

Object Oriented Programming

Java Programming Language. 0 A history

Java Application Development

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

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

Creating a C++ Program

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

a. a * c - 10 = b. a % b + (a * d) + 7 =

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CMP 326 Midterm Fall 2015

CS Week 14. Jim Williams, PhD

Chapter 9 Lab Text Processing and Wrapper Classes

CS 163/164 - Exam 2 Study Guide and Practice Written Exam

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Transcription:

CS 163/164 Exam 2 Review

Review from first exam

What does this print? String s = marco polo ; System.out.println(s.substring(0,3));

mar

Print the predefined double variable d with 9 decimal place precision (with a new line).

System.out.printf( %.9f\n, d);

Create a Scanner that reads in a word from the keyboard. Store the word in a String variable called wordsbro.

Scanner s = new Scanner (System.in); String wordsbro = s.next();

Why do you need to have an extra.nextline() when you are trying to read a full line after calling.next(),.nextint(), or.nextdouble()?

token processing to line processing..nextline looks for a new line character ( \n ) in a line, so after reading a word, int, double, etc there is still a \n character to read, so you must eat the rest of the line to read the next line.

Loops

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) + & );

Write a for loop that prints the reverse of the String variable s.

for (int i = s.length() -1; i >= 0; i--) System.out.print(s.charAt(i));

Write a while loop that prints the numbers 3 9 (inclusive) separated by new lines.

int i = 3; while (i <= 9) { System.out.println(i); i++; }

Write a do-while loop that will add the sum of all numbers from 1-19 (inclusive).

int count = 1; int sum = 0; do { sum += count; count++; } while(count < 20);

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? (Specify which ones cause errors and which ones are just incorrect (but they compile)? A. for (int i = 0; i <= s.length(); i++) B. for (int i = 0; i < s.length()+1; i++) C. for (int i = 0; i < s.length(); i++) D. for (int i = 0; i <= s.length() -1; i++) E. 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

Arrays

Declare and allocate a String array called sarray to be of size 10.

String [] sarray = new String [10];

Declare and initialize an int array called iarray with the values 1, 2, 3 (in that order).

int [] iarray = {1, 2, 3};

Given the predefined 1D String array called stringarray. Print the length of the array (with a new line).

System.out.println(stringArray.length);

Declare and allocate a 4x7 2D char array called letters.

char [][] letters = new char [4][7];

Print each element of the predefined 2D byte array called b (every element should be printed on the same line, with a new line at the very end)

for (int i = 0; i < b.length; i++) for (int j = 0; j < b[i].length; j++) System.out.print(b[i][j]); System.out.println();

Declare and assign a 3x3 2D double array, called doubletable, with all of the values assigned to 2.0.

double [] [] doubletable = new double [3][3]; for (int i = 0; i < doubletable.length; i++) for (int j = 0; j < doubletable[i].length; j++) doubletable[i][j] = 2.0; OR double [] [] doubletable = {{2.0, 2.0, 2.0}, {2.0, 2.0, 2.0}, {2.0, 2.0, 2.0}}; // spacing doesn t change anything

What does the following code print?

prints the February calendar 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

What does this code print? Note: ASCII value of a is 97 and b is 98.

[97, 97, 97, 97, 97, 98, 98, 98, 98, 98]

Methods

1. What is the return value of this method? 2. What does this method do? 3. How would I call this method?

1.char 2.returns the last character of s 3.Foo( somestring ); or Foo(someStringVariable);

1. What is the return value of this method? 2. How would I print the result of this method (in the main method)?

1. boolean 2. System.out.println(fullOCleverNames());

Create a public static method called caster, that returns a double, it takes an int as a parameter. Return the double, caused from typecasting the parameter.

public static double caster (int i) { return (double)i; }

Create a public static method called printme, that returns nothing and takes a float as a parameter. Print the float with 8 decimal points with a new line.

public static void printme (float f0) { System.out.printf( %.8f\n, f0); }

Create a public static method stub (no code inside) called practice, that returns a char and takes an int and a String as a parameter.

public static char practice (int i, String s); // you can have any parameter variable names

Create a public static method stub (no code inside) called practice1, that returns nothing and has no parameters.

public static void practice1 ();

What does the following code print?

[3, 6, 9, 12, 15]

Objects

Make a StudentData object called student1 with id: 123, first name: James, last name: Bond public class StudentData { private int id; private String firstname, lastname; public StudentData (int num, String first, String last) { id = num; firstname = first; lastname = last; } }

StudentData student1 = new StudentData (123, James, Bond );

Declare and allocate an array of StudentData objects called CS163 to have a size of 3. public class StudentData { private int id; private String firstname, lastname; public StudentData (int num, String first, String last) { id = num; firstname = first; lastname = last; } }

StudentData [] CS163 = new StudentData [3];

Use the following information to create three StudentData objects and store into the CS163 array from Part A. - 1 Rob Drobs - 2 Bob Crobs - 3 Steve Jobs public class StudentData { private int id; private String firstname, lastname; public StudentData (int num, String first, String last) { id = num; firstname = first; lastname = last; } }

CS163[0] = new StudentData (1, Rob, Drobs ); CS163[1] = new StudentData(2, Bob, Crobs ); CS163[2] = new StudentData(3, Steve, Jobs ); OR StudentData sd1 = new StudentData(1, Rob, Drobs ); CS163[0] = sd1; StudentData sd2 = new StudentData(2, Bob, Crobs ); CS163[1] = sd2; StudentData sd3 = new StudentData(3, Steve, Jobs ); CS163[2] = sd3;

Write a tostring method such that when an object is printed the format is: id, lastname, firstname public class StudentData { private int id; private String firstname, lastname; public StudentData (int num, String first, String last) { id = num; firstname = first; lastname = last; } }

@Override public String tostring () { return id +, + lastname +, + firstname; } OR @Override public String tostring () { return this.id +, + this.lastname +, + this.firstname; } Note: Override is optional, but suggested

What gets printed?

Title: null Author: null Year: 0

What gets printed?

On my machine: Movie@6d06d69c

When do you need to use the keyword this?

When you are accessing class variables that have the same name as a local variable. E.g. if (this.name == otherobject.name) OR public class Student { private String name; public Student (String name) { this.name = name; } }