Programming Languages CRN Test 2 Version 1 CMSC 4023 Autumn 2013

Similar documents
Programming Languages Test 3 Version 1 CMSC 4023 CRN Autumn 2012

Programming Languages Test 2 Revision 1 CMSC 4023 CRN Spring 2007

Programming I Test 2 Version 1 CS 1613 Summer 2011

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Chapter 6. Data Types ISBN

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

FORM 2 (Please put your name and form # on the scantron!!!!)

Tokens, Expressions and Control Structures

HANDLING NONLOCAL REFERENCES

The New C Standard (Excerpted material)

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Chapter 6. Data Types ISBN

22c:111 Programming Language Concepts. Fall Types I

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Programming Languages Third Edition. Chapter 7 Basic Semantics

Organization of Programming Languages CS3200 / 5200N. Lecture 06

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Types. What is a type?

Data Types In Text: Ch C apter 6 1

Operators and Expressions:

Review of the C Programming Language for Principles of Operating Systems

CHAPTER 3 Expressions, Functions, Output

Data Types and Variables in C language

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Review of the C Programming Language

UNIT- 3 Introduction to C++

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Introduction to C++ Systems Programming

Concepts Introduced in Chapter 6

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

1 Lexical Considerations

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Programming I Test 1 CS 1613 Spring 2004

PROGRAMMING IN C++ COURSE CONTENT

Introduction to Programming (Java) 4/12

CS2141 Software Development using C/C++ C++ Basics

Pointers, Dynamic Data, and Reference Types

Array Initialization. Rectangular and Jagged Arrays. Arrays Operations. ICOM 4036 Programming Languages. Data Types

Topic 9: Type Checking

Topic 9: Type Checking

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

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

Programming I Test 2 Revision 1 CS 1613 Spring 2004

The SPL Programming Language Reference Manual

TYPES, VALUES AND DECLARATIONS

Introduction to C# Applications

Programming Languages, Summary CSC419; Odelia Schwartz

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Types II. Hwansoo Han

Expressions and Assignment

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Computers Programming Course 6. Iulian Năstac

Lexical Considerations

Java Notes. 10th ICSE. Saravanan Ganesh

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Fundamentals of Programming

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

CS201- Introduction to Programming Current Quizzes

Fundamentals of Programming Languages

MIDTERM EXAMINATION - CS130 - Spring 2003

Constructor - example

Operators in C. Staff Incharge: S.Sasirekha

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

Chapter 7 Control I Expressions and Statements

Introduce C# as Object Oriented programming language. Explain, tokens,

G Programming Languages - Fall 2012

Chapter 7. Additional Control Structures

Expressions & Assignment Statements

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Fundamental of Programming (C)

Informatics Ingeniería en Electrónica y Automática Industrial

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Lecture 12: Data Types (and Some Leftover ML)

6.096 Introduction to C++ January (IAP) 2009

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

A data type defines a collection of data objects and a set of predefined operations on those objects

Lexical Considerations

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Concepts Introduced in Chapter 6

3. Java - Language Constructs I

The New C Standard (Excerpted material)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Transcription:

1. Print your name on your scantron in the space labeled NAME. 2. Print CMSC 4023 in the space labeled SUBJECT. 3. Print the test number and version, T2/V1, in the space labeled TEST NO. 4. Print the date, 10-30-2013, in the space labeled DATE. 5. Print your CRN number, 12105, in the space labeled PERIOD. 6. This is a closed-book examination. No reference materials are permitted. No notes are permitted. 7. You may not consult your neighbors, colleagues, or fellow students to answer the questions on this test. 8. Cellular phones are prohibited. The possessor of a cellular phone will receive a zero (0) if the phone rings or is visible during the test. 9. Mark the best selection that satisfies the question. If selection b is better that selections a and d, then mark selection b. Mark only one selection. 10. Darken your selections completely. Make a heavy black mark that completely fills your selection. 11. Answer all 25 questions. 12. Record your answers on SCANTRON form 882-E (It is green!) 1

1. (Ch. 5. p 222) Select the correct storage type for variable m. #include <iomanip> int max(int A[],int N) { int m=a[0]; for (int i=1;i<n;i++) if (A[i]>m) m=a[i]; return m; { int A[]={17,2,-3,11,5,-7,-13 cout << max(a,7) << endl; Figure 1 Program for Question 1 a. Static variable b. Stack-Dynamic variable c. Explicit Heap-Dynamic variable d. Implicit Heap-Dynamic variable 2. (Ch. 5. p. 233) What is printed by the program in Figure 2 assuming dynamic scope? int a,b; void print(void){cout << "a=" << a << " b=" << b; int p(int& a){int p=2;a=0;b=1;return p; void q(void){int b=4;a=3;print(); {a=p(a);q(); Figure 2 C++ Program for Question 2 a. a=1 b=3 b. a=2 b=4 c. a=3 b=1 d. a=3 b=4 2

3. (Ch. 5 p. 225) What is printed by the program in Figure 3 assuming static scope? int a,b; void print(void){cout << "a=" << a << " b=" << b; int p(int& a){int p=2;a=0;b=1;return p; void q(void){int b=4;a=3;print(); {a=p(a);q(); Figure 3 C++ Program for Question 3 a. a=3 b=4 b. a=2 b=4 c. a=1 b=3 d. a=3 b=1 4. (Ch. 5. p 216) Identify the binding type illustrated by the JavaScript statements shown in Figure 5. list = [10.2,3.5]; list=47; Figure 5 C++ Program for Question 5 a. dynamic b. static c. inferential d. explicit 3

5. (Ch. 5. p 222) What is the lifetime of variable p declared on line 6 of Figure 5? 1. 2. 3. int a; 4. int b; 5. int p(int& a) 6. { int p=2; 7. a=0; 8. b=1; 9. return p; 10. 11. void q(void) 12. { static int b=4; 13. a=3; 14. 15. 16. { a=p(a); q(); 17. 18. Figure 5 Program for Question 5 a. Storage for variable p is allocated when function main is called and its storage is reclaimed when function main returns. b. Storage for variable p is allocated during translation and reclaimed when the file containing executable form of the program in figure 5 is deleted. c. Storage for variable p is allocated when function p is called and its storage is reclaimed when function p returns. d. Storage for variable p is allocated at load time and reclaimed when the program in Figure 5 returns control to the operating system. 6. (Ch. 5. p. 214) When is the range of possible values for a variable of type int assigned in the programming language C++? a. Execution time b. Translation time c. Language definition time d. Language implementation time 7. (Ch. 6. p. 259) Which of the following declarations does NOT define an ordinal type? a. enum color {red, green, blue b. type day is ordinal (Mon,Tue,Wed,Thu,Fri,Sat,Sun); c. subtype Index is Integer range 1..100; d. char 4

8. (Ch. 6. p. 277) Assume that each element of array A occupies e bytes, array A has r rows whose indexes are 1, 2,, r and c columns whose indexes are 1, 2,, c. Array A is allocated in row-major order. Without subscripts A is the address of the first byte allocated. Which of the following expressions gives the address of the first by of element A[i][j]? a. AA + ((ii 11) cc + jj 11) ee b. AA + ii cc ee + jj ee c. AA + ((jj 11) cc + ii 11) ee d. AA + jj cc ee + ii ee 9. (Ch. 6. p. 256) Which of the following is NOT a string length option discussed in our text? a. static length string b. limited dynamic length string c. dynamic length string d. null-terminated string 10. (Ch. 6. p. 307) Which kind of type checking makes type X equivalent to type Y? type X=array[1..10] of integer; type Y=array[1..10] of integer; a. dynamic b. referential c. structural d. static 5

11. (Ch. 6. p. 288) Which set of C++ declarations most closely represents the Ada declarations given in the diagram below? type Shape is (Circle,Triangle,Rectangle); type Colors is (Red,Green,Blue); type Figure (Form:Shape) is record Filled:Boolean; Color:Colors; case Form is when Circle => Diameter:Float; when Triangle => Left_Side:Integer; Right_Side:Integer; Angle:Float; when Rectangle => Side_1:Integer; Side_2:Integer; end case; end record; Figure 11 Declarations for Question 11 6

enum Shape {Circle,Triangle,Rectangle enum Colors {Red,Green,Blue struct Type_Triangle { int Left_Side,Right_Side; float Angle; struct Type_Rectangle { int Side_1,Side_2; struct Type_Circle { float Diameter; struct Type_Form { Type_Circle C; Type_Triangle T; Type_Rectangle R; struct Figure { bool Filled; Colors Color; Shape Form; Type_Form F; Figure 11 a enum Shape {Circle,Triangle,Rectangle enum Colors {Red,Green,Blue struct Type_Triangle { int Left_Side,Right_Side; float Angle; struct Type_Rectangle { int Side_1,Side_2; struct Type_Circle { float Diameter; union Type_Form { Type_Circle C; Type_Triangle T; Type_Rectangle R; struct Figure { bool Filled; Colors Color; Shape Form; Type_Form F; Figure 11 b 7

enum Shape {Circle,Triangle,Rectangle enum Colors {Red,Green,Blue union Type_Form { int Side_1,Side_2; float Diameter; int Left_Side,Right_Side; float Angle; struct Figure { bool Filled; Colors Color; Shape Form; Type_Form F; Figure 11 c enum Shape {Circle,Triangle,Rectangle enum Colors {Red,Green,Blue struct Type_Triangle { int Left_Side,Right_Side; float Angle; struct Type_Rectangle { int Side_1,Side_2; struct Type_Circle { float Diameter; struct Type_Form { Type_Circle C; Type_Triangle T; Type_Rectangle R; union Figure { bool Filled; Colors Color; Shape Form; Type_Form F; Figure 11 d 8

12. (Ch. 6. p. 293) Which code fragment produces a dangling pointer? int* f(void) { int d; return &d; { int* p=f(); Figure 12 a { int* p=new int; int* q=p; delete q; Figure 12 c int* f(void){return new int; { int* p=f(); int q; p=&q; Figure 12 b int* f(void){return new int; { int* p=f(); p=new int; Figure 12 d 13. (Ch. 6. p. 306) Which feature of C++ is primarily responsible for characterizing the language as NOT strongly typed? a. mixed-mode coercion b. user-defined operator overloading c. polymorphic pointers d. union types 14. (Ch. 7. p. 324) According to Sebesta, what operator usually associates to the right? a. exponentiation operator ** b. unary minus - c. assignment = d. prefix increment ++ 9

15. (Ch. 7. p. 332) Which of the following code fragments contains an example of coercion? #include <string> { string s="tomat"; char c='o'; s=s+c; Figure 15 a { char A=0x20; A = A << 1 0x01; cout << A << endl; Figure 15 c { double x=1.0; double y=2.0; int i=(int)(x+y); { double x=1.0; double y=x+1; Figure 15 b Figure 15 d 16. (Ch. 7. p. 337) Identify the order of evaluation for the expression given in the Figure below? Please note that expressions are evaluated in the order in which they appear from left to right. Expressions are separated by commas in the selections below. int a=0; int b=5; a>=0 b<5?a=5:b=0; Figure 16 Expression for Question 16 a. a>=0, b<5, a=5, b=0 b. a>=0, a=5 c. a>=0, b<5, a=5, d. a>=0, b<5, b=0 10

17. (Ch. 7. p. 321) Assume the following rules of associativity and precedence for expressions. Operators Precedence Associativity *, /, not highest left to right +, - & mod left to right - (unary) right to left =, /=, <, <=, >=, > left to right and left to right or, xor lowest left to right Show the order of evaluation of the expression in the Figure below by parenthesizing all sub-expressions and placing a superscript on the right parenthesis to indicate the order. For example, for the expression a + b * c + d the order of evaluation would be represented as ((a + (b * c) 1 ) 2 + d) 3 (a b) / c & (d * e / a 3) Figure 17 Expression for Question 17 a. (((a b) 1 / c) 2 & ((d * e) 3 / (a 3) 4 ) 5 ) 6 b. ((a b) 1 / c (& (d * (e / (a 3) 2 ) 3 ) 4 ) 5 ) 6 c. (((a b) 1 / (c & ((d * e) 2 / a) 3 ) 6 3) 4 ) 5 d. (((d * e / a 3) 2 / c ) 5 & (((d * e) 2 /a) 3-3)) 4 ) 6 11

18. (Ch. 7. p. 321) Mark the selections that satisfy the relation given in Figure 18.1 for the grammar of Figure 18.2. Function p (op) returns the precedence of the argument op. Argument op is an operator in the grammar given in Figure 18.2. The operators given in the grammar of Figure 18.2 include +, -, unary -, *, /, and ^. The operator, unary-, appears in production 8. pp(oooo 11 ) pp(oooo 22 ) pp(oooo nn ) oooo ii {+,,, /,, uuuuuuuuuu Figure 18.1 a. pp(+) pp(uuuuuuuuuu ) pp( ) b. pp(+) pp( ) pp( ) pp( ) c. pp(uuuuuuuuuu ) pp( ) pp( ) d. pp( ) pp( ) pp(/ ) pp( ) 1 expression term 2 expression term addop expression 3 term factor 4 term factor mulop term 5 factor power 6 factor power powop factor 7 power ( expression ) 8 power - power 9 power id 10 addop + 11 addop - 12 mulop * 13 mulop / 14 powop ^ Figure 18.2 12

19. (Ch. 7. p. 328) What is printed by the program given in the Figure below if operands on lines 9 and 11 are evaluated right to left? 1. 2. 3. int fun(int* k) 4. { *k += 4; 5. return 3 * (*k) -1; 6. 7. 8. { int i=5,j=7,sum1,sum2; 9. sum1=(i/2)+fun(&i); 10. cout << " sum1=" << sum1; 11. sum2=fun(&j)+(j/2); 12. cout << " sum2=" << sum2; 13. cout << endl; 14. 15. Figure 19 Program for Question 19 a. sum1=30 sum2=37 b. sum1=28 sum2=37 c. sum1=30 sum2=35 d. sum1=28 sum2=35 20. (Ch. 8. p. 359) What programming language feature illustrated in the code fragment below is prohibited in C#? switch (value) { case -1: Negatives++; break; case 0: Zeros++; goto case 1; case 1: Positives++; default: Console.WriteLine( Default\n ); Figure 20 Code Fragment for Question 20 a. implicit execution of more than one selectable segment b. unary assignment operators ++ and --. c. goto statements d. negative case values. 13

21. (Ch. 8. p. 373) What programming language contains the grammar given below for a forstatement? for-statement for-statement for loop-variable in object statement for loop-variable in object statement else statement a. CLU. b. Ada c. Perl d. Python 22. (Ch. 8. p. 368) What is the value of variable Count after the Ada code fragment given below exits the loop? Count: Float:=1.35; for Count in 1..10 loop Sum:=Sum+Count; end loop; Figure 22 Program for Question 22 a. 10.35 b. The code fragment fails to compile because Ada does not permit a floating point variable to be a loop control variable. c. 1.35 d. 11 23. (Ch. 8. p. 354) In what language is the following example prohibited? if (sum==0) if (count==0) result=0; else result=1; Figure 23 Code Fragment for Question 23 a. Perl b. Java c. C# d. C++ 14

24. (Ch. 8. p. 353) What is printed when the program below is executed? { int sum=0,result; if (sum=1) result=0; else result=1; cout << "sum=" << sum << " result=" << result; cout << endl; Figure 24 Program for Question 24 a. sum=0 result=0 b. sum=0 result=1 c. sum=1 result=0 d. sum=1 result=1 25. (Ch. 8. p. 383) What is the relationship between integer variables m1, m2, and m3 after exiting the loop containing guarded command proposed by Dkjkstra shown below? do m1 > m2 -> t:=m1; m1:=m2; m2:=t; [] m2 > m3 -> t:=m2; m2:=m3; m3:=t; od Figure 25 Program for Question 25 a. mm1 mm2 mm3 b. mm3 mm2 mm1 c. mm1 < mm2 < mm3 d. mm3 < mm2 < mm1 15