Department of Computer Science Purdue University, West Lafayette

Similar documents
Computer System and programming in C

Computational Expression

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS & IT Conversions. Magnitude 10,000 1,

Getting started with Java

Use the Scantron sheets to write your answer. Make sure to write your Purdue ID (NOT Purdue Login ID) and your name on the Scantron answer sheet.

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

Full file at

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

b. Suppose you enter input from the console, when you run the program. What is the output?

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Computer Systems Programming. Practice Midterm. Name:

1 Short Answer (15 Points Each)

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

CS16 Exam #1 7/17/ Minutes 100 Points total

Question: Total Points: Score:

Number System. Introduction. Decimal Numbers

Computer Science II Fall 2009

CS1800 Discrete Structures Final Version A

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Chapter 3: Arithmetic for Computers

AP Computer Science Unit 1. Programs

CSC201, SECTION 002, Fall 2000: Homework Assignment #2

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

COMP2611: Computer Organization. Data Representation

Chapter 2: Basic Elements of Java

Java Basics Programming Fundamentals

Input. Scanner keyboard = new Scanner(System.in); String name;

CS Week 5. Jim Williams, PhD

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Midterm Examination (MTA)

Floating Point Arithmetic

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 11 Introduction to Programming in C

UNIVERSITY OF WISCONSIN MADISON

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Introduction to Java Applications

Exercise: Using Numbers

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

More Programming Constructs -- Introduction

CS101 Introduction to computing Floating Point Numbers

CS1800 Discrete Structures Fall 2017 October 25, CS1800 Discrete Structures Midterm Version B

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Section 2: Introduction to Java. Historical note

Number representations

CEN 414 Java Programming

CSC 1351: Exam 2: Midterm

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Chapter 11 Introduction to Programming in C

Formatted Output / User IO

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Full file at

Introduction to Computer Science-103. Midterm

Chapter 4. Operations on Data

Beyond Base 10: Non-decimal Based Number Systems

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Introduction to Computer Science Unit 2. Notes

Unified Engineering Fall 2004

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

As stated earlier, the declaration

CS 101 Exam 1 Spring 200 Id Name

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

CS101 Lecture 04: Binary Arithmetic

Test 1. CSC 121 Lecture Lecturer: Howard Rosenthal. March 4, 2014

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Birkbeck (University of London) Department of Computer Science and Information Systems. Introduction to Computer Systems (BUCI008H4)

PROGRAMMING FUNDAMENTALS

Handout 4 Conditionals. Boolean Expressions.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Introduction to Computer Science Unit 2. Notes

Chapter 11 Introduction to Programming in C

Language Reference Manual

The type of all data used in a C++ program must be specified

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.


This exam is open book. Each question is worth 3 points.

Chapter 5 : Computer Arithmetic

Reading Input from Text File

Introduction to Computer Science Midterm 3 Fall, Points

The Math Class. Using various math class methods. Formatting the values.

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CS1800 Discrete Structures Fall 2017 October 25, CS1800 Discrete Structures Midterm Version B

Key Java Simple Data Types

Number Systems. Both numbers are positive

CS 251 Intermediate Programming Java Basics

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Question: Total Points: Score:

AP Computer Science A Summer Assignment

Java Coding 3. Over & over again!

Digital Signal Processing Introduction to Finite-Precision Numerical Effects

Transcription:

Department of Computer Science Purdue University, West Lafayette Fall 2011: CS 180 Problem Solving and OO Programming Exam 1 Solutions Q 1 Answer the questions below assuming that binary integers are represented in 5-bits using 2 s complement. The leftmost bit indicates the sign of the number. For real numbers simply provide your answer using as many bits needed for the integer part of the number and only 4- bits for the fraction part of the number. You do not need to normalize the number or represent it in the IEEE format. (a) Binary equivalent of 12 10 = 01100 (b) Decimal equivalent of 11111 2 =-1 (c) Binary sum of 0011 2 + 0100 2 =0111 (d) Binary equivalent of 9.7 10 =1001.1011 (e) Binary equivalent of hexadecimal number 3A 16 = Cannot be represented in 5 bits; otherwise 0111010 (f) Largest integer (decimal value)=15 (g) Smallest integer (decimal value)=-16

Page 2 Q 2 You are required to write a Java program that performs the following tasks in the sequence specified below. (a) Prompt the user to enter a number and read the number typed. The user may type any valid number. (b) Prompt the user to enter another number and read the number typed. Again, the user may type any valid number. [5 points ] (c) Prompt the user for the operation to be performed. The user will select one of two operations: add (+) or multiply (*). The user will type a + if an add operation is to be performed and a * if a multiply operation is to be performed. (d) Perform the operation specified by the user on the two numbers the program has already input. (e) Display the result of applying the operation. The result must be a number of type double and displayed to only 2-digit accuracy and displayed as indicated in the examples below. Note that all numbers are displayed to 2-digit accuracy. Here is an example of how your program should interact with the user. 20.0, 3.5, and * are sample inputs provided by the user in the first example below. Use the prompts given below. All else on the lines below is output generated by your program. Ignore the possibility that the user input may make your program result in an overflow or underflow. Enter a number: 20.0 Enter another number:3.5 Enter operation to be performed (+ or *): * 20.00*3.50=70.00 And below is another example. Enter a number: -3.3356 Enter another number: 1 Enter operation to be performed (+ or *): + -3.34+1.00=-2.34

Listing 1: Program for Q2. (Calculate.java) Page 3 1 2 3 import java. util.*; 4 public c l a s s Calculate { 5 6 public s t a t i c void main ( String [] args ){ 7 double num1, num2 ; // To hold input numbers. 8 String opstring ;// To hold operator. 9 char op; // To hold character corresponding to the operator. 10 Scanner s =new Scanner ( System. in ); 11 System. out. print (" Please enter a number : "); 12 num1 = s. nextdouble (); // Get the first number, 13 System. out. print (" Please enter another number : "); 14 num2 =s. nextdouble (); // and the second one. 15 System. out. print (" Please enter an operator (+ or *): "); 16 opstring = s. next (); // There is no nextchar () method. 17 op = opstring. charat ( 0); // Extract the operator from the string. 18 19 double result =0; // Initialize result to avoid compile error. 20 i f (op == + ){ 21 result = num1 + num2 ; // Add 22 } 23 i f (op == * ){ 24 result = num1 * num2 ; // Multiply 25 } 26 // Output formatted result. If the operator is 27 // not * or + then a 0 will be output. 28 System. out. printf (" %.2 f%c %.2 f = %.2 f\n", num1, op, 29 num2, result ); 30 }// End of main () 31 }// End of Calculate

Page 4 Q 3 You are required to write a Java program that reads lines of text from the console one after the other. For each line read, the program checks if the first and the last character on that line are the same. If they are not the same the program ignores that line, If the first and the last characters on the line read are the same then the program prints the first character on that line on the console and terminates. The example below should help you better understand what your program is supposed to do. Input read from the console How do you do? Program action Do not print this line because the first and the last characters on this, i.e., H and o, line are not the same. CS 180 is challenging! Do not print this line because the first and the last characters, i.e., C and!, are not the same. *ABC% This line is also not to be printed as the first and the last characters are not the same. Sator Arepo Tenet Opera RotaS The first and the last characters are the same. Hence print the character S and terminate. Note again that your program must continue to read lines of text from the input until the user types a text in which the first and the last characters are identical.

Listing 2: Program for Q3. (FirstLastChar.java) Page 5 1 2 3 import java. util.*; 4 public c l a s s FirstLastChar { 5 6 public s t a t i c void main ( String [] args ){ 7 String line =""; // Holds line input. 8 Scanner s =new Scanner ( System. in ); // Create a Scanner object. 9 char first = ; // Holds first character. 10 char last = ; // Holds second character. 11 boolean done = f a l s e ; // Indicates if reading should stop. 12 while (! done ){ 13 line =s. nextline (); // Read next line 14 i f ( line. length ()!=0){ // No action if the line empty. 15 first = line. charat ( 0); // Extract first char and 16 last = line. charat ( line. length () -1); // the last char 17 i f ( first == last && first!= ){ // Ignore space at the start 18 done = true ; 19 } 20 }// End of empty line check. 21 }// End of input reading loop. 22 System. out. println ( first ); // Display the first character 23 }// End of main () 24 }// End of FirstLastChar <End of Exam 1 Solutions>