CS Computers Programming II Review of CS 101 Dr. H. Assadipour

Similar documents
MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Introduction to Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Java. CS 3: Computer Programming in Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Part I. Multiple Choice Questions (2 points each):

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

J a v a Quiz (Unit 3, Test 0 Practice)

In this Chapter you ll learn:

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

AP Computer Science Java Mr. Clausen Program 9A, 9B

Sample CSE8A midterm Multiple Choice (circle one)

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Primitive data types in Java

Using Files as Input/Output in Java 5.0 Applications

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Basics of Java Programming Input and the Scanner class

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

(Eng. Hayam Reda Seireg) Sheet Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

AP Computer Science Static Methods, Strings, User Input

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Lab 5: Bank Account. Defining objects & classes

JAVA ARRAY EXAMPLE PDF

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Homework/Program #5 Solutions

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Topic 11 Scanner object, conditional execution

More on Objects and Classes

Chapter 2: Elements of Java

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Contents. 9-1 Copyright (c) N. Afshartous

Inheritance, overloading and overriding

COMPUTER SCIENCE. Paper 1 (THEORY)

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Chapter 2 Introduction to Java programming

Comp 248 Introduction to Programming

AP Computer Science Java Subset

objectives chapter Define classes that act like blueprints for new objects, made of variables and methods. Explain encapsulation and Java modifiers.

Compile and Runtime Errors in Java

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

Introduction to Programming

CS114: Introduction to Java

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Chapter 2 Elementary Programming

CS 111 Classes I 1. Software Organization View to this point:

CS170 Lab 11 Abstract Data Types & Objects

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

D06 PROGRAMMING with JAVA

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

The following program is aiming to extract from a simple text file an analysis of the content such as:

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

1. Writing Simple Classes:

Conditionals (with solutions)

Building Java Programs

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Programming Fundamentals I CS 110, Central Washington University. November 2015

Building Java Programs

Crash Course in Java

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Building Java Programs

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

java Features Version April 19, 2013 by Thorsten Kracht

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Java Basics: Data Types, Variables, and Loops

Java Classes. GEEN163 Introduction to Computer Programming

Introduction to Java Lecture Notes. Ryan Dougherty

Object-Oriented Programming in Java

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Chapter 3. Input and output. 3.1 The System class

Visit us at

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

CSE 1020 Introduction to Computer Science I A sample nal exam

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Install Java Development Kit (JDK) 1.8

Algorithms and Data Structures Written Exam Proposed SOLUTION

Moving from CS 61A Scheme to CS 61B Java

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

CS 121 Intro to Programming:Java - Lecture 11 Announcements

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

LOOPS CHAPTER CHAPTER GOALS

Arrays. Introduction. Chapter 7

Transcription:

CS 102 - Computers Programming II Review of CS 101 Dr. H. Assadipour 1. What is the output of this program? public class Q_01 int x=3; int y=5; double z=9; System.out.println("a: " + y/x); System.out.println("b: " + z/x); System.out.println("c: " + y%x); x=y; y=x; System.out.println("d: " + x); System.out.println("e: " + y); 2. What is the output of this program? public class Q_02 for(int i=0; i<3; i++) System.out.println(i); for(int j=i; j<3; j++) System.out.print(j + "\t"); System.out.println(); a: 1 b: 3.0 c: 2 d: 5 e: 5 0 0 1 2 1 1 2 2 2 3. What is the output of this program? public class Q_03 public static void main( String args[] ) int y, x = 0, total = 0; while ( x < 10 ) y = (int)math.pow(x,2); System.out.println("y = " + y ); total += y; x+=2; System.out.println( "Total is: " + total ); y = 0 y = 4 y = 16 y = 36 y = 64 Total is: 120 1

4. What is the output of this program? public class Q_04 int n=1; while(n<=128) n=2*n; System.out.println(n); 2 4 8 16 32 64 128 5. What is the output of this program? public class Q_05 int[] a=1,2,6,3, 5; for(int i=0; i<a.length; i++) System.out.print(a[i]+ "\t"); System.out.println(); for(int i=0; i<a.length; i++) a[i]=a[i]+i; for(int i=0; i<a.length; i++) System.out.print(a[i] + "\t"); 256 1 2 6 3 5 1 3 8 6 9 6. Consider the array declaration double [] x = new double[5]; (a) How many elements does x have? (b) What is the type of x[3]? (c) What is the smallest index i such that x[i] is valid? (d) What is the largest index i such that x[i] is valid? (e) What is the value of x.length? (a) 5 (b) double (c) 0 (d) 4 (e) 5 2

7. Suppose you are designing a class. (a) What would you use to represent the state of an object? (b) What are accessor methods used for? (c) What are constructors used for? (d) Why is it useful to implement tostring()? (e) Why is it useful to make instance variables private? 8. What is the output of this program? public class Q_08 int x=0; for(int i=1; i<=6; i++) x=x+i; System.out.println("i = " + i + "\t" + x); System.out.println("The final answer: " + x); 9. What is the output of this program? public class Q_09 int x = 3; switch(x) case 3: x += 3; case 4: break; case 5: x += 2; System.out.println("x = " + x); case 6: x ++; case 7: x +=2; case 8: x --; case 9: x +=3; System.out.println("x = " + x); (a) data (b) getters/readers (c) instantiation (d) nice display of object s contents (e) encapsulation i = 1 1 i = 2 3 i = 3 6 i = 4 10 i = 5 15 i = 6 21 The final answer: 21 X = 6 3

10. What is the output of the following program? import java.util.scanner; public class Q_10 public static void main (String[] args) int ans = 35, x = 50, y = 50; if(x > y) ans = x + 10; x -= y; else ans = y + 10; y += x; System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("ans = " + ans); 11. What is the output of the following program? public class Q_11 String s1 = "abc"; String s2 = "def"; String s3 = s1; s2 = "xyz"; System.out.println(s1 + \n + s2 + \n + s3); 12. What is the output of the following program? public class Q_12 int x= 0; int y= 0; for (int z = 0; z <= 5; z++) if (( ++x > 1 ) && (++y > 1)) x++; System.out.println("x = " + x + "\t" + "y = " + y); 13. What is the output of the following program? public class Q_07 int i1 = 5; String s1 = "7"; int i2 = 6; System.out.println(i1 + i2 + s1); Your Answer: x = 50 y = 100 ans = 60 Your Answer: abc xyz abc Your Answer: x = 10 y = 5 Your Answer: 117 4

14. Write a program to display the dimensions of a letter-size (8.5 x 11 inches) sheet of paper in millimeters. There are 25.4 millimeters per inch. Use constants and comments in your program. import java.util.scanner; public class Q_14 float w, h; Scanner input = new Scanner(System.in); System.out.print("Please enter w and h: "); w = input.nextfloat(); h = input.nextfloat(); w *=25.4; h *=25.4; System.out.println("Dimensions in mm are: " + w + "\t" + h); Please enter w and h : 11 8.5 Dimensions in mm are: 279.4 215.9 15. Write a program to read integers until a zero is input. The program then prints the sum of the integers that were input and the number of negative integers that were input. import java.util.scanner; public class Q_15 int x, sum = 0, Neg_Counter = 0; Scanner input = new Scanner(System.in); System.out.print("Enter an ineger: "); x = input.nextint(); while(x!=0) System.out.print("Enter an ineger: "); x = input.nextint(); sum += x; if (x<0) Neg_Counter++; Enter an ineger: 8 Enter an ineger: 7 Enter an ineger: 8 Enter an ineger: -5 Enter an ineger: -6 Enter an ineger: 0 Sum = 4 # of negative #'s = 2 System.out.println(" Sum = " + sum); System.out.println(" # of negative #'s = " + Neg_Counter); 5

16. Write a program that reads an array of integers, e.g, [23, 34, 24, 33, 12] and displays the average and the standard deviation of the elements of the array. You must include two static methods one named mean (for the mean) and one named std_dev (for standard deviation). mean n x n i ( ) 2 i 1 x i 1 i mean n std _ dev Show a sample output with the expected results. n 1 import java.text.decimalformat; public class Q_16 int[] a = 23, 34, 24, 33, 12; DecimalFormat fmt = new DecimalFormat("0.000"); System.out.println("Mean = " + fmt.format (mean(a, a.length))); System.out.println("Std Dev = " + fmt.format(standarddeviation(a, a.length))); public static double mean (int[] numbers, int count) double sum = 0.0; for (int i=0; i < count; i++) sum += numbers[i]; return sum / count; public static double standarddeviation (int[] numbers, int count) double result = 0.0; double mean = mean(numbers, count); for (int i=0; i < count; i++) result += Math.pow (numbers[i] - mean, 2); return Math.sqrt (result / count-1); Mean = 25.200 Std Dev = 7.922 6

17. Given the following program, answer the following questions: import java.text.decimalformat; class SphereTest // Creates and exercises some Sphere objects. public static void main (String[] args) DecimalFormat fmt = new DecimalFormat("0.00"); Sphere s1 = new Sphere (5); System.out.println (s1); s1.setdiameter(9); System.out.println (s1); System.out.println("The area = " + fmt.format(s1.area(s1.getdiameter()))); System.out.println("The volume = " + fmt.format(s1.volume(s1.getdiameter()))); public static class Sphere private int diameter; // Sets up this Sphere object with the specified data. public Sphere (int diam) diameter = diam; // Accessors/Getters/Read & Mutators/Setters/Write public int getdiameter () return diameter; public void setdiameter (int diam) diameter = diam; public double area (int diameter) return Math.PI*Math.pow(diameter, 2); The area = 254.47 public double volume (int diameter) return (Math.PI/6.)*Math.pow(diameter, 3); public String tostring () DecimalFormat fmt = new DecimalFormat("0.00"); return "Diameter: " + diameter + "\t Area = " + fmt.format(area(diameter))+ "\t Volume = " + fmt.format(volume(diameter)) ; Sample run: a. Name of the Driver program: b. Name of the class: c. Constructor s name: d. An Accessor method s name: e. A Mutator method s name: f. An object s name created in the driver: g. An object passed onto print/println invokes: Diameter: 5 Area = 78.54 Volume = 65.45 Diameter: 9 Area = 254.47 Volume = 381.70 The volume = 381.70 7

18. The following program uses an array as input to a method (FindMax) and returns the maximum value. public class Seven int [] values = 23, 78, 14, 56, 90, 101, 32, 34; System.out.println("Max = " + FindMax (values)); public static int FindMax(int[] x) int largest = x[0]; for(inti = 1; i<x.length; i++) if(x[i] > largest) largest = x[i]; return largest; Max = 101 19. The following program searches for a value in an array, if found replaces it with the last element of the array. Import java.util.scanner; public class Five intpos = 0; Please enter a value: 56 int [] values = 23, 78, 14, 56, 90,101,32, 34; Scanner input = new Scanner(System.in); Found at position: 3 System.out.print("Please enter a value: "); intsearchvalue = input.nextint(); 23 78 14 34 90 101 32 34 boolean found = false; while(pos<values.length&&!found) if(values[pos] == searchvalue) found = true; else pos++; if(found) System.out.println("Found at position: " + pos); else System.out.println("Was not found"); If(found == true) values[pos] = values[values.length-1]; for(inti = 0; i<values.length; i++) System.out.print(values[i] + " "); 8