Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Similar documents
Memory Allocation. General Questions

Huawei Test 3. 1 A card is drawn from a pack of 52 cards. The probability of getting a queen of club or a king of heart is:

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Huawei Test 5. Explanation: P.W. of Rs. 12,880 due 8 months hence=rs. [ (12880*100)/(100+(18*(8/12)))]

Essar Placement Paper

Sasken Technical Questions

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Principles of C and Memory Management

Fundamental of Programming (C)

Huawei Test 2. 1 Two dice are thrown simultaneously. What is the probability of getting two numbers whose product is even?

Fundamentals of Programming Session 20

Computer Programming Unit 3

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

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

Lab 3. Pointers Programming Lab (Using C) XU Silei

Introduction to C Language (M3-R )

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

Unit IV & V Previous Papers 1 mark Answers

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

AMCAT Automata Coding Sample Questions And Answers

AMCAT Procedure functions and scope Sample Questions

We will introduce another operator & using the example below

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

Library Functions. General Questions

Government Polytechnic Muzaffarpur.

Solutions to Assessment

CS201- Introduction to Programming Current Quizzes

Goals of this Lecture

{ int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } A. Function addmult() return 7 and 12 B. No output C. Error: Compile error D.

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

M1-R4: Programing and Problem Solving using C (JAN 2019)

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

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

APSC 160 Review Part 2

PROGRAMMAZIONE I A.A. 2017/2018

Pointers, Arrays, and Strings. CS449 Spring 2016

Multi-Dimensional arrays

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Content. In this chapter, you will learn:

Introduction to C. Systems Programming Concepts

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

Why VC++ instead of Dev C++?

Fundamentals of Programming Session 19

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

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

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CS 61c: Great Ideas in Computer Architecture

4) In C, if you pass an array as an argument to a function, what actually gets passed?

Arrays and Pointers. CSE 2031 Fall November 11, 2013

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Character Strings. String-copy Example

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Arrays and Strings. Arash Rafiey. September 12, 2017

COMPUTER APPLICATION

Unit 7. Functions. Need of User Defined Functions

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Arrays and Pointers (part 1)

Programming Language B

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers (part 1)

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Pointers and Arrays 1

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

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

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Subject: Fundamental of Computer Programming 2068

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Introduction to Scientific Computing and Problem Solving

Chapter 7 Functions. Now consider a more advanced example:

ESC101N: Fundamentals of Computing End-sem st semester

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

ESC101N Fundamentals of Computing

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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

Dynamic memory allocation

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

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

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

High Performance Programming Programming in C part 1

Exam 3 Chapters 7 & 9

Array Initialization

Programming Studio #9 ECE 190

by Pearson Education, Inc. All Rights Reserved.

Transcription:

1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would report an error. C. The program may crash if some important data gets overwritten. D. The array size would appropriately grow. Answer: Option C If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors. Example: Run the below program, it will crash in Windows (TurboC Compiler) int arr[2]; arr[3]=10; printf("%d",arr[3]); Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output. Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better. 2. What does the following declaration mean? int (*ptr)[10]; A. ptr is array of pointers to 10 integers B. ptr is a pointer to an array of 10 integers C. ptr is an array of 10 integers D. ptr is an pointer to array 3. In C, if you pass an array as an argument to a function, what actually gets passed? A. Value of elements in array B. First element of the array C. Base address of the array D. Address of the last element of array

2 Answer: Option C The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed. Find Output of Program 1. What will be the output of the program? int a[5] = 5, 1, 15, 20, 25; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); A. 2, 1, 15 B. 1, 2, 5 C. 3, 2, 15 D. 2, 3, 20 Answer: Option C Step 1: int a[5] = 5, 1, 15, 20, 25; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25. Step 2: int i, j, m; The variable i,j,m are declared as an integer type. Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2 Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3. Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3) Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m Hence the output of the program is 3, 2, 15 2. What will be the output of the program? static int a[2][2] = 1, 2, 3, 4;

int i, j; static int *p[] = (int*)a, (int*)a+1, (int*)a+2; for(i=0; i<2; i++) for(j=0; j<2; j++) printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), *(*(i+p)+j), *(*(p+j)+i)); 1, 1, 1, 1 2, 3, 2, 3 A. 3, 2, 3, 2 4, 4, 4, 4 1, 1, 1, 1 2, 2, 2, 2 C. 2, 2, 2, 2 3, 3, 3, 3 Answer: Option C 3. What will be the output of the program? 3 1, 2, 1, 2 2, 3, 2, 3 B. 3, 4, 3, 4 4, 2, 4, 2 1, 2, 3, 4 2, 3, 4, 1 D. 3, 4, 1, 2 4, 1, 2, 3 void fun(int, int[]); int arr[] = 1, 2, 3, 4; int i; fun(4, arr); for(i=0; i<4; i++) printf("%d,", arr[i]); void fun(int n, int arr[]) int *p=0; int i=0; while(i++ < n) p = &arr[i]; *p=0; A. 2, 3, 4, 5 B. 1, 2, 3, 4 C. 0, 1, 2, 3 D. 3, 2, 1 0 Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything. Step 2: int arr[] = 1, 2, 3, 4; The variable a is declared as an integer array and it is initialized to

4 a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4 Step 3: int i; The variable i is declared as an integer type. Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function. Step 5: for(i=0; i<4; i++) printf("%d,", arr[i]); The for loop runs untill the variable i is less than '4' and it prints the each value of array a. Hence the output of the program is 1,2,3,4 4. What will be the output of the program? void fun(int **p); int a[3][4] = 1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0; int *ptr; ptr = &a[0][0]; fun(&ptr); void fun(int **p) printf("%d\n", **p); A. 1 B. 2 C. 3 D. 4 Answer: Option A Step 1: int a[3][4] = 1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0; The variable a is declared as an multidimensional integer array with size of 3 rows 4 columns. Step 2: int *ptr; The *ptr is a integer pointer variable. Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer variable *ptr. Step 4: fun(&ptr); Now, the &ptr contains the base address of array a. Step 4: Inside the function fun(&ptr); The printf("%d\n", **p); prints the value '1'. because the *p contains the base address or the first element memory address of the array a (ie. a[0]) **p contains the value of *p memory location (ie. a[0]=1). Hence the output of the program is '1' 5. What will be the output of the program?

static int arr[] = 0, 1, 2, 3, 4; int *p[] = arr, arr+1, arr+2, arr+3, arr+4; int **ptr=p; ptr++; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); *ptr++; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); *++ptr; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); ++*ptr; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); 0, 0, 0 1, 1, 1 A. 2, 2, 2 3, 3, 3 1, 1, 1 2, 2, 2 C. 3, 3, 3 3, 4, 4 Answer: Option C 5 1, 1, 2 2, 2, 3 B. 3, 3, 4 4, 4, 1 0, 1, 2 1, 2, 3 D. 2, 3, 4 3, 4, 5 6. What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes? int a[3][4] = 1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0; printf("%u, %u\n", a+1, &a+1); A. 65474, 65476 B. 65480, 65496 C. 65480, 65488 D. 65474, 65488 Step 1: int a[3][4] = 1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions. Step 2: printf("%u, %u\n", a+1, &a+1); The base address(also the address of the first element) of array is 65472. For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480 Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496 Hence the output of the program is 65480, 65496 6 7. What will be the output of the program in Turb C (under DOS)? int arr[5], i=0; while(i<5) arr[i]=++i; for(i=0; i<5; i++) printf("%d, ", arr[i]); A. 1, 2, 3, 4, 5, B. Garbage value, 1, 2, 3, 4, C. 0, 1, 2, 3, 4, D. 2, 3, 4, 5, 6, Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the TurboC Compiler (Windows) output. Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better. 8. What will be the output of the program? int arr[1]=10; printf("%d\n", 0[arr]); A. 1 B. 10 C. 0 D. 6 Step 1: int arr[1]=10; The variable arr[1] is declared as an integer array with size '2' and it's first element is initialized to value '10'(means arr[0]=10)

Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr. Hence the output of the program is 10. 7 9. What will be the output of the program if the array begins at address 65486? int arr[] = 12, 14, 15, 23, 45; printf("%u, %u\n", arr, &arr); A. 65486, 65488 B. 65486, 65486 C. 65486, 65490 D. 65486, 65487 Step 1: int arr[] = 12, 14, 15, 23, 45; The variable arr is declared as an integer array and initialized. Step 2: printf("%u, %u\n", arr, &arr); Here, The base address of the array is 65486. => arr, &arr is pointing to the base address of the array arr. Hence the output of the program is 65486, 65486 10. What will be the output of the program? float arr[] = 12.4, 2.3, 4.5, 6.7; printf("%d\n", sizeof(arr)/sizeof(arr[0])); A. 5 B. 4 C. 6 D. 7 The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes Step 1: float arr[] = 12.4, 2.3, 4.5, 6.7; The variable arr is declared as an floating point array and it is initialized with the values.

8 Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0])); The variable arr has 4 elements. The size of the float variable is 4 bytes. Hence 4 elements x 4 bytes = 16 bytes sizeof(arr[0]) is 4 bytes Hence 16/4 is 4 bytes Hence the output of the program is '4'. 11. What will be the output of the program if the array begins 1200 in memory? int arr[]=2, 3, 4, 1, 6; printf("%u, %u, %u\n", arr, &arr[0], &arr); A. 1200, 1202, 1204 B. 1200, 1200, 1200 C. 1200, 1204, 1208 D. 1200, 1202, 1200 Step 1: int arr[]=2, 3, 4, 1, 6; The variable arr is declared as an integer array and initialized. Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here, The base address of the array is 1200. => arr, &arr is pointing to the base address of the array arr. => &arr[0] is pointing to the address of the first element array arr. (ie. base address) Hence the output of the program is 1200, 1200, 1200 Point Out Correct Statements 1. Which of the following is correct way to define the function fun() in the below program? int a[3][4]; fun(a); void fun(int p[][4]) A. B. void fun(int *p[4])

C. void fun(int *p[][4]) void fun(int *p[3][4]) D. Answer: Option A 9 void fun(int p[][4]) is the correct way to write the function fun(). while the others are considered only the function fun() is called by using call by reference. 2. Which of the following statements mentioning the name of the array begins DOES NOT yield the base address? 1: When array name is used with the sizeof operator. 2: When array name is operand of the & operator. 3: When array name is passed to scanf() function. 4: When array name is passed to printf() function. A. A B. A, B C. B D. B, D The statement 1 and 2 does not yield the base address of the array. While the scanf() and printf() yields the base address of the array. 3. Which of the following statements are correct about the program below? int size, i; scanf("%d", &size); int arr[size]; for(i=1; i<=size; i++) scanf("%d", arr[i]); printf("%d", arr[i]); A. The code is erroneous since the subscript for array used in for loop is in the range 1 to size. B. The code is erroneous since the values of array are getting scanned through the loop. C. The code is erroneous since the statement declaring array is invalid. D. The code is correct and runs successfully.

10 Answer: Option C The statement int arr[size]; produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here. Example: int arr[10]; One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]; is placed after a function call scanf(). 4. Which of the following statements are correct about 6 used in the program? int num[6]; num[6]=21; A. In the first statement 6 specifies a particular element, whereas in the second statement it specifies a type. In the first statement 6 specifies a array size, whereas in the second statement it specifies a particular B. element of array. In the first statement 6 specifies a particular element, whereas in the second statement it specifies a array C. size. D. In both the statement 6 specifies array size. The statement 'B' is correct, because int num[6]; specifies the size of array and num[6]=21; designates the particular element(7 th element) of the array. 5. Which of the following statements are correct about an array? 1: The array int num[26]; can store 26 elements. 2: The expression num[1] designates the very first element in the array. 3: It is necessary to initialize the array at the time of declaration. 4: The declaration num[size] is allowed if SIZE is a macro. A. 1 B. 1,4 C. 2,3 D. 2,4 1. The array int num[26]; can store 26 elements. This statement is true. 2. The expression num[1] designates the very first element in the array. This statement is false, because it designates the second element of the array.

3. It is necessary to initialize the array at the time of declaration. This statement is false. 11 4. The declaration num[size] is allowed if SIZE is a macro. This statement is true, because the MACRO just replaces the symbol SIZE with given value. Hence the statements '1' and '4' are correct statements. Yes / No Questions 1. A pointer to a block of memory is effectively same as an array A. True B.False Answer: Option A Yes, It is possible to allocate a block of memory (of arbitrary size) at run-time, using the standard library's malloc function, and treat it as an array. 2. Does this mentioning array name gives the base address in all the contexts? A. Yes B.No No, Mentioning the array name in C or C++ gives the base address in all contexts except one. Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression. When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value". 3. Is there any difference int the following declarations? int fun(int arr[]); int fun(int arr[2]); A. Yes B.No No, both the statements are same. It is the prototype for the function fun() that accepts one integer array as an

12 parameter and returns an integer value. 4. Are the expressions arr and &arr same for an array of 10 integers? A. Yes B.No Both mean two different things. arr gives the address of the first int, whereas the &arr gives the address of array of ints.