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

Similar documents
Dynamic Memory Allocation and Command-line Arguments

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

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

C PROGRAMMING QUESTIONS AND

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

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

ECE264 Fall 2013 Exam 3, November 20, 2013

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Essar Placement Paper

CS 0449 Sample Midterm

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

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Midterm Exam Review Slides

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

PRINCIPLES OF OPERATING SYSTEMS

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Bristol Institute of Technology

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

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

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Dynamic memory allocation

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Exercise Session 2 Simon Gerber

Chapter 11 Introduction to Programming in C

COMP s1 Lecture 1

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Computer Systems Programming. Practice Midterm. Name:

Work relative to other classes

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Getting started with Java

Array Initialization

COMP 2355 Introduction to Systems Programming

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Overview (1A) Young Won Lim 9/25/17

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

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

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Chapter 11 Introduction to Programming in C

A Short Course for REU Students Summer Instructor: Ben Ransford

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

15213 Recitation Section C

CIS 2107 Computer Systems and Low-Level Programming Fall 2010 Midterm

CS Programming In C

COMP2611: Computer Organization. Data Representation

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CGS 3460 Summer 07 Midterm Exam

Chapter 11 Introduction to Programming in C

As stated earlier, the declaration

Problem Set 1: Unix Commands 1

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

C Language Summary (Continued)

Topic 6: A Quick Intro To C

A Fast Review of C Essentials Part I

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! Solution. printed

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Lecture 03 Bits, Bytes and Data Types

EL2310 Scientific Programming

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

CPS 101 Introduction to Computational Science

ECE 250 / CPS 250 Computer Architecture. C Programming

Programming in C. What is C?... What is C?

Library Functions. General Questions

Programming in C UVic SEng 265

CSE 124 Discussion (10/3) C/C++ Basics

Final CSE 131B Spring 2004

Chapter 11 Introduction to Programming in C

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS356: Discussion #3 Floating-Point Operations. Marco Paolieri

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

CSE 351. Introduction & Course Tools

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Programming in C. What is C?... What is C?

Computer System and programming in C

AMCAT Automata Coding Sample Questions And Answers

Chapter 11 Introduction to Programming in C

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

Transcription:

COMP 2001/2401 Test #1 [out of 80 marks] Duration: 90 minutes Authorized Memoranda: NONE Note: for all questions, you must show your work! Name: Student#: 1. What exact shell command would you use to: [6 marks] a. change a directory s permissions to: drwxr-x--x chmod 751 dir-name also valid: chmod u=rwx,g=rx,o=x dir-name b. change a file s permissions to: -rw-r----- chmod 640 filename also valid: chmod u=rw,g=r,o= filename 2. Given the following C code: [8 marks] what is output: -12 char c = 0xf4; printf("%d", c); - convert 0xf4 to binary - apply two s complement - have correct answer, including sign 1/6

3. Given the following C code: [10 marks] char x = 189; unsigned char y = x + 0x4f; printf("%d %d", x, y); what is output: -67 12 Option 1: - convert 189 to binary - use two s complement to get proper decimal value: -67 - convert 0x4f to decimal: 79 - add both to get 12 Option 2: - convert 189 to binary - use two s complement to get proper decimal value: -67 - convert 0x4f to binary - do binary addition to get 12 4. Show the binary representation of the following floating point number: -52.375 [7 marks] Answer: 1100 0010 0101 0001 1000 0000 0000 0000 - sign bit: 1 - fixed point: -52.375 = 2 5 + 2 4 + 2 2 + 2-2 + 2-3 = 110100.011 = 1.10100011 x 2 - exponent: 5 + 127 = 132 = 1000 0100 - fraction: 1010 0011 5 2/6

5. Given the following C code: [4 marks] int j=10, k; k = --j; printf("%d ", k); k = j--; printf("%d ", k); what is output: 9 9 6. Which bit model is not used by data types in C? why? [6 marks] signed-magnitude bit model 1). there are two possible bit values for zeros: 0000 0000 and 1000 0000 2). binary addition is somewhat complicated 7. Given the following code, [20 marks] [Bonus marks] struct Collection char name[5]; unsigned char id; int year; double price; struct Collection books [2]; books[0].id = 2; books[0].year = 2012; books[0].price = 100.32; strcpy(books[0].name, "XI"); books[1].price = 20.9; strcpy(books[1].name, "XII"); the memory map for this code could be written as follows. It is assumed that the addresses indicate the sizes of the variables, in bytes. 3/6

Address label(s) Label(s) Address Value books books[0].name books[0] 400 'X' books[0].name[0] books[0].name[1] 401 'I' books[0].name[2] 402 '\0' books[0].name[3]-[4] 403-404 books[0].id 405 2 books[0].year 406-409 2012 books[0].price 410-417 100.32 books[1].name books[1] 418 'X' books[1].name[0] books[1].name[1] 419 'I' books[1].name[2] 420 'I' books[1].name[3] 421 '\0' books[1].name[4] 422 books[1].id 423 books[1].year 424-427 books[1].price 428-435 20.9 8. Given the following code: [9 marks] char str[20]; strcpy(str, "Hello"); strcpy(&str[6], " world"); a. fill in each cell below with the contents of the str array after the statements execute: H e l l o 0 w o r l d 0 b. assuming that this additional statement executes: strcat(&str[7], " Timmy"); fill in each cell below with the new contents of the str array: H e l l o 0 w o r l d T i m m y 0 4/6

c. assuming that the statement in part (b) has executed, what is the output of printf("%s",str): "Hello" 9. Given a complete C source code called test.c for a program, [13 marks] shown in the following: #include <stdio.h> int main (int argc, char *argv[]) int count; printf ("This program was called with \"%s\".\n",argv[0]); if (argc > 1) for (count = 1; count < argc; count++) printf("argv[%d] = %s\n", count, argv[count]); else printf("the command had no other arguments.\n"); return 0; Instructions: (1) what exact shell command you would use to compile and run the code? [3 marks] The executable file name is called test.out. gcc test.c -o test.out (2) after compiling successfully, you have an executable file called test.out. You are going to give two tests for this executable file by running this executable file under Linux shell window to cover two kinds of conditions used by 'if' control structure. - First, write down the command for running this executable file; [2 marks] - second, write down its output. [3 marks] Test condition 1:./test.out This program was called with./test.out. The command had no other arguments. 5/6

Test condition 2:./test.out -option1 -option2 This program was called with./test.out. argv[1] = -option1 argv[2] = -option2 10. You are writing a small program to print a series triangle as follows. For this test, you will be filling in the missing portions in the program, whose goal is to manage the loop and print out the triangle displaying all those numbers, as found in the sample output below. [17 marks] Instructions: Read and understand the program provided as follows, which was used to generate the sample output Fill in the missing portions of the program Note: with the code that you fill in, the program must be complete, i.e. make no assumptions about "magic" functions that don't exist! Note: assume the input is valid and the input value is in the range of [1, 9]. 6/6

Source code for the program printing a series triangle #include <stdio.h> void main() int i, j, k, m = 1, num; // supply an integer number here printf("insert an integer \n"); scanf("%d", &num); [2 marks] printf("print a series triangle:\n"); // print a series triangle for(i = 1, j = num -i; i <= num; i++, j--) for(k = 1; k <= j; k++) printf(" "); for(k = 1; k <= m; k++) printf("%d", i); printf(" \n"); m = m + 2; [5 marks] [3 marks] [3 marks] [1 marks] [1 marks] [2 marks] 7/6