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

Similar documents
CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

First of all, it is a variable, just like other variables you studied

AMCAT Automata Coding Sample Questions And Answers

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

C: How to Program. Week /Mar/05

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Chapter 2 - Introduction to C Programming

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

بسم اهلل الرمحن الرحيم

printf( Please enter another number: ); scanf( %d, &num2);

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out

Final CSE 131B Spring 2004

Computer Systems Programming. Practice Midterm. Name:

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 1 & 2 Introduction to C Language

Introduction to C An overview of the programming language C, syntax, data types and input/output

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Fundamentals of Programming Session 8

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Fundamentals of Programming Session 4

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Introduction to C Language

Reserved Words and Identifiers

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Programming in C Quick Start! Biostatistics 615 Lecture 4

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Computer System and programming in C

Question 2. [5 points] Given the following symbolic constant definition

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Syntax and Variables

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

C Program Development and Debugging under Unix SEEM 3460

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

A Fast Review of C Essentials Part I

Technical Questions. Q 1) What are the key features in C programming language?

H192 Midterm 1 Review. Tom Zajdel

Control Structure: Loop

Program Organization and Comments

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

BSM540 Basics of C Language

C introduction: part 1

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Programming for Engineers Introduction to C

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

CS 61c: Great Ideas in Computer Architecture

Lecture 03 Bits, Bytes and Data Types

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Subject: Fundamental of Computer Programming 2068

ET156 Introduction to C Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Arrays and Strings. Arash Rafiey. September 12, 2017

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

EL2310 Scientific Programming

Procedures, Parameters, Values and Variables. Steven R. Bagley

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CC112 Structured Programming

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

The C Programming Language Guide for the Robot Course work Module

BİL200 TUTORIAL-EXERCISES Objective:

Arithmetic Expressions in C

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CS 61C: Great Ideas in Computer Architecture Introduction to C

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Introduction to Computer Science Midterm 3 Fall, Points

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Chapter 3. Fundamental Data Types

Structured Programming. Dr. Mohamed Khedr Lecture 4

(3-1) Functions II H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (September 5, 2018) Washington State University

Memory layout. int foo() { int b; } int main() { int a; foo(); Program. Stack. Heap. foo: int b main: int a

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Advanced C Programming Topics

Functions. Arash Rafiey. September 26, 2017

Module 4: Decision-making and forming loops

Data types, variables, constants

Creating, Compiling and Executing

6.096 Introduction to C++ January (IAP) 2009

Exercise: Using Numbers

Chapter 5: Control Structures

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Transcription:

CS16 Exam #1 7/17/2012 75 Minutes 100 Points total Name: 1. (10 pts) Write the definition of a C function that takes two integers `a` and `b` as input parameters. The function returns an integer holding the value a 2 + b 2. Write ONLY the function; not an entire program. int function( int a, int b ) { return ( a * a ) + ( b * b );

2. (10 pts) Let `input` and `ret` be integer variables. Consider the following code: ret = 0; switch ( input ) { case 1: ret = ret + 1; case 2: ret = ret + 2; case 4: case 5: ret = ret + 4; case 6: ret = ret + 6; case 7: ret = ret + 7; case 8: ret = ret + 8; default: ret = -1; Complete the following table asking what the value of `ret` is after executing this code for a given value of `input`. Note that these are independent runs, so `ret` always starts at 0. If `input` is... Then `ret` will be... 0-1 1 1 2 2 3-1 4 4 5 4 6 13 7 7 8-1 9-1

3. (10 pts) Given the integer variables `ret`, `x`, `y`, and `z`, write an if/else if/else construct that will assign the largest of `x`, `y`, and `z` to `ret`. In other words, `ret` = max( max( `x`, `y` ), `z` ). Do NOT use any built-in max() function or define your own max() function. if ( x >= y && x >= z ) ret = x; else if ( y >= x && y >= z ) ret = y; else ret = z; 4. (2 pts) What would most likely happen if the following code were to be run? Why? (Hint: %s is the placeholder for strings.) int x = 10; printf( %s, x ); It d probably crash. `x` would be treated as an address of a string (i.e. where the string is in memory).

5. (3 pts) What is wrong with the following code? void foo(); int x = 0; int y; void foo() { if ( x > 5 ) return; x++; y = foo(); foo() doesn t return anything (has a void return type), but we attempt to assign its return value to `y`. 6. (20 pts) For each of the following C expressions, clearly mark the type of the expression and the value of the expression. The first two are provided as examples. Expression Value Type 16 16 int double char char* 0.2 * 0.2 0.04 int double char char* 15 % 4 3 int double char char* 4 % 15 4 int double char char* 2 + 3 * 3 11 int double char char* 2 + 2 / 4.0 2.5 int double char char* 1.0 / 10 0.1 int double char char* 9 % 3 0 int double char char* 2 + 7.0 2 + 7.0 int double char char* 2 2 int double char char* 2 / 3 0 int double char char* 5 + (char)2 7 int double char char*

7. (20 pts) There is a partially complete program below that can calculate force (in N) given mass (in kg) and acceleration (m / s 2 ). It includes the function definition `calcforce`, which performs the actual force calculation to a result in N. The program still needs the following components: a. Code that prompts the user for input (using `printf`) asking for mass and acceleration b. Code that reads in mass and acceleration from the user (using `scanf`) c. Code that calls `calcforce` to get the actual result d. Code that prints out the result of `calcforce` e. Anything else required so it will compile Sample Run Hints -bash-2.05b$./prog Enter mass (kg): 1.5 Enter acceleration (m/s^2): 3.0 Force: 4.5 N -bash-2.05b$ The printf/scanf placeholder for doubles is %lf Use function prototypes whenever a function is used before it is defined Remember to tell C that we are using the standard input / output library After the program completes, the prompt for bash should be on a new line // program for converting mass and acceleration to force #include <stdio.h> double calcforce( double massinkg, double accinmss ); int main() { double mass, acc; printf( Enter mass (kg): ); scanf( %lf, &mass ); printf( Enter acceleration (m/s^2): ); scanf( %lf, &acc ); printf( Force: %.1lf N\n, calcforce( mass, acc ) ); // bonus if you used the.1 part for precision return 0; double calcforce( double massinkg, double accinmss ) { return massinkg * accinmss;

8. (2 pts) What does the following code print? int x = 0; if ( x = 0 ) { printf( true ); else { printf( false ); false (assignment instead of equality used) 9. (2 pts) What are the values of the variables `x` and `y` after the following code is run? int x = 0; int y = x++ + 1; x is 1, y is 1 10.(3 pts) Considering an unsigned char that is one byte in size: a. What is the smallest value? 00000000 b. What is the largest value? 11111111 c. What is 128? 10000000 11.(2 pts) Show how the string foo is represented in memory in C, byte by byte. You may use the C single quote notation for representing individual characters (i.e. the character c is represented in C with c.) f, o, o, \0

12.(4 pts) Convert the decimal number `5` to binary (i.e. to base 2). 101 13.(10 pts) For each one of the following error conditions, say what kind of error it is (considering the four types of errors discussed in class): a. There is a missing semicolon between two printf statements. Syntax error b. The program attempts to divide an integer by 0. Runtime error c. A function in math.h was used but the programmer forgot to tell C that the math library was being used. Linker error d. The formula for a particular calculation was written down incorrectly. Logic error e. An attempt is made to increment an unsigned variable that is one byte large and holding the value 255. Runtime error 14.(2 pts) Give two reasons why a programmer should use symbolic constants. (i.e. constants declared using #define) -Only need to change them in one place -Gives semantic meaning to the values