CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Similar documents
CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

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

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

Notes for Unit 1 Part A: Rational vs. Irrational

Computer System and programming in C

Flow Control. CSC215 Lecture

Decision Making in C

CS-211 Fall 2017 Test 2 Version A November 29, Name:

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

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

CSc Introduction to Computing

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Binghamton University. CS-211 Fall Functions. The Basis of C

Creating, Compiling and Executing

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

C: How to Program. Week /Mar/05

Pointers and scanf() Steven R. Bagley

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Lecture 3: C Programm

Informatica e Sistemi in Tempo Reale

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

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

Fundamentals of Programming

Lecture 5: Methods CS2301

Chapter 2 - Introduction to C Programming

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

This is CS50. Harvard University Fall Quiz 0 Answer Key

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

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name:

Following is the general form of a typical decision making structure found in most of the programming languages:

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

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

UNIT III (PART-II) & UNIT IV(PART-I)

Peer Instruction 1. Elementary Programming

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Computer Programming. Decision Making (2) Loops

Structured Programming. Functions and Structured Programming. Functions. Variables

Topic 1: Introduction

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

PROGRAMMAZIONE I A.A. 2017/2018

Simple Java Reference

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

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

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Programming in C Quick Start! Biostatistics 615 Lecture 4

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP 2001/2401 Test #1 [out of 80 marks]

CS1100 Introduction to Programming

Chapter 11 Introduction to Programming in C

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

York University Faculty Science and Engineering Fall 2008

BRANCHING if-else statements

UNIT- 3 Introduction to C++

Introduction to Computing Systems Fall Lab # 3

Introduction. C provides two styles of flow control:

EL2310 Scientific Programming

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

Course Outline Introduction to C-Programming

Computer Organization & Systems Exam I Example Questions

Computer Programming

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

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

Number Representation & Conversion

CS 0449 Sample Midterm

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Functions. Arash Rafiey. September 26, 2017

Chapter 11 Introduction to Programming in C

A Fast Review of C Essentials Part I

Structured Programming. Jon Macey

CS201- Introduction to Programming Current Quizzes

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

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

Chapter 11 Introduction to Programming in C

Array. Array Declaration:

( &% class MyClass { }

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Lecture 2 Tao Wang 1

(Not Quite) Minijava

1/29/2018. Starting a Program Executes its main Function. ECE 220: Computer Systems & Programming. The Function main Divides into Two Parts

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

COMP 2355 Introduction to Systems Programming

Dept. of CSE, IIT KGP

Lecture 03 Bits, Bytes and Data Types

Programming for Engineers Iteration

Basic Elements of C. Staff Incharge: S.Sasirekha

CS111: PROGRAMMING LANGUAGE II

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

Reserved Words and Identifiers

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

Transcription:

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, 2017 True/False Questions... Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics, we learned that every integer, n has a successor, n + 1, that is greater than n. This is also true in the C language. In C, integers must have a type, which controls the number of bits used to represent that number, and since the number of bits is finite, there are limits on the size of an integer that can be represented in C. (b) T F : A single C code file, compiled on different machines, can produce different results. The C compiler translates the C code to the machine language of the machine it is running on. While the intent of the C code is normally followed, there are enough differences in machine languages so that it is impossible to guarantee that a program will run the same on different machines. (c) T F : If aliens from outer space prevent all computers from typing (or cut and pasting) the character (right curly brace), then no one could write any new C code. In C, almost all statements must be in a function, and it is impossible to create a function without using a right curly brace. (d) T F : Lines in C code that start with a # (pound sign) are pre-processor directives, and do not follow the normal C syntax requirements. (e) T F : All variables in C have a name, a value, a type, and a location in memory. (f) T F : There are four different types of integers in C so that we can save memory. Use char for small integers, short for medium size integers, int for normal integers, and long for larger integers. (g) T F : A C statement must fit on a single line. Multiple statements can appear on one line, or one statement may span multiple lines. (h) T F : If the left hand side of an assignment is a char, the value of the right hand side is truncated to 8 bits. (i) T F : It is legal to invoke a function as long as that function is declared and defined in the same C program file, even if the declaration comes after the invocation. A function must be declared before it can be invoked. (j) T F : Almost all code in C is contained inside functions, where a function has a name, a return type, a parameter list, and a function body. 2. (10 points) For the following, put a check in front of the statements that are are valid constant expressions. 32 768 9934 0x1ABd 6.23e22 so called 0340721 x 0b729 CS-211 4.729 Page 1 of 5

3. (10 points) For the following, put a check in front of the statements that are valid declaration statements. int i = 37 295; int j=1,024; float pi=3.1514; double break=5.0; float fraction=7; unsigned short width=-3; int = a ; double bignumber=inf; float constant=3.5e0x16; int num cookies=093; 4. (10 points) Given the following declare statements: int num=17; f loat area ; char l i t t l e ; Put a check in front of the statements that are syntactically correct, and will not cause a compile error. num=12 * height; area = num * 12.835e0; little = 1.43e7; if (12.3>=area) then printf( Not that big. ); do num++ while (num<32); area * 13 = 772.6; area = width times height; little = (12 % num) num; for(num>6; ++num; num=-num) num - -; littler=little-1; 5. (10 points) Given the following declarations: int a = 7 ; int b = 3 ; int c = 4 ; float fx =2.5; float fy = 1.1 e0 ; Determine the value of the following C expressions. Make sure you include a decimal point for float values, but leave a decimal point out for integer values. (a) 10 = a + b (b) 10.0 = c * fx (c) 7 = (c = 7) (d) 1 = a % 3 (e) a = fx 0? a : b (f) 1 = b 17 (g) 40 = (a+b) * c (h) 3 = b 1 Page 2 of 5

6. (10 points) What does the following code print when compiled and executed? int a = 1 3 ; int b = ++a ; i f (++b < ++a ) { p r i n t f ( Condition 1\n ) ; else { i f ( b > a ) { p r i n t f ( Condition 2\n ) ; else { i f (++b < a++) { p r i n t f ( Condition 3\n ) ; else { p r i n t f ( Condition 4\n ) ; p r i n t f ( a=%d, b=%d\n, a, b ) ; Condition 4 a=15, b=15 7. (10 points) What does the following code print when compiled and executed? char a = 0 b01000100 ; int pos =0; char mask=0b00000001 ; while ( mask ) { i f ( mask & a ) { p r i n t f ( Bit %d i s on\n, pos ) ; mask <<=1; pos++; Bit 2 is on Bit 6 is on Page 3 of 5

8. (20 points) Answer the questions below based on the following C program: #include <s t d i o. h> / Round up the usual s u s p e c t s / #include <s t d l i b. h> int nextfactor ( int n ) ; int main ( int argc, char argv ) { i f ( argc!= 2) { // I f not e x a c t l y one argument p r i n t f ( Invoke as : %s <number>\n, argv [ 0 ] ) ; return 1 ; // q u i t with an e r r o r return code int n = a t o i ( argv [ 1 ] ) ; // Save the u s e r s argument as i n t e g e r n p r i n t f ( %d =, n ) ; // Print n= int f = nextfactor ( n ) ; // Find the f i r s t f a c t o r while ( f < n ) { p r i n t f ( %d x, f ) ; // p r i n t out t h i s f a c t o r n = n / f ; // Remove t h i s f a c t o r from n f = nextfactor ( n ) ; // Find the next f a c t o r p r i n t f ( %d\n, f ) ; // p r i n t out the l a s t f a c t o r return 0 ; // T e l l OS e v e r y t h i n g i s c o p a c e t i c int nextfactor ( int n ) { int f = 2 ; while ( f f<= n ) { i f (0== n%f ) return f ; // I f f d i v i d e s n e v e n l y f = f + 1 ; return n ; (a) Why does the code that states int nextfactor(int n) appear twice in this program? To avoid upside-down code - nextfactor must be declared before it is used. (b) Would this program work if the variable f in the nextfactor fucntion was initialized to 1 instead of 2? If not, why not? nextfactor would always return 1, since 1%n=0 for any n. The loop in main would never end. (c) What would this program print if compiled and invoked as./primefact 30? 30 = 2 x 3 x 5 (d) When this program prints factors of a number, what order are those factors in? Least to Greatest, Greatest to Least, or Random? Least to Greatest Page 4 of 5

9. (10 points) Write a function in C that takes two integers, a and b, as parameters, and returns the average of the two parameters, rounded to the nearest integer using a round towards zero rounding policy. (Hint: This is much easier than it sounds.) Solution: int avg ( int a, int b ) { return ( a+b ) / 2 ; Question: 1 2 3 4 5 6 7 8 9 Total Points: 10 10 10 10 10 10 10 20 10 100 Bonus Points: 0 0 0 0 0 0 0 0 0 0 Page 5 of 5