CSCI-0046 Systems Programming in Linux - MIDTERM

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

True or False: int data[10 ]; In this array, we can store different data types

False

True or False: Once an array is defined with a certain size, we can always decrease or increase the size depending upon how the program works.

False

all data cells in this array will have a value of 4 int data [ 6 ] = { 4 } ;

False

if ( 0 ) printf ( "Yes \n" ); else printf ( "No\n"); The above statement would print Yes. True or False

False

int data [ 3 ] ; data++ ; // Is this a valid statement

False

int data1 [10 ] ; int data2 [10 ]; data1 = data2; // is this possible ?

False

int x = 10; if ( x = 0 ) printf ( "Yes\n") else printf ( "No\n"); printf ( "x=%d \n", x ) ; The value of x printed is 10 True or False

False

statement 1: for ( i = 0 ; i < 20 ; i++ ) continue; printf ( "i = %d \n", i) ; statement 2: for ( i = 0 ; i < 10 ; ++i ) continue; printf ( "i = %d \n", i) ; both statements produce the same result. True or False

False

Which of the following expressions results in false value A. true && false B. true && true C. true || true D. false || true

A

The following statement will get compilation error int data[4]; data++ ;

True

if ( -10 ) printf ( "Yes \n" ); else printf ( "No\n"); The above statement would print Yes. True or False

True

int data [ ] = { 8, 6, 4, 2 } ; data++ ; you cannot do data++ on an array name.

True

int data [ ] = { 8, 6, 4, 2 } ; int * ptr = &data[0]; printf ( "%d", *ptr ) ; will print 8

True

int data [3 ] = { 3, 4, 5 }; int data2 [ 3 ] ; data2 = data ; // THIS IS ILLEGAL. You cannot assign an array to another array

True

int i = 0; int x = 1; for ( ; i < x ; break ) i+=10 ; printf ( "i=%d \n", i ); The above program clip will not compile. True

True

statement 1: for ( i = 0 ; i < 10 ; i++ ) printf ( "\n"); statement 2: for ( i = 0 ; i < 10 ; ++i ) printf ( "\n"); Both statements will produce the same result . True or False

True

statement 1: for ( i = 0 ; i < 20 ; i++ ) break; statement 2: for ( i = 0 ; i < 10 ; ++i ) break; both statements produce same result . True or False

True

int * ptr = 20 ; int y = *ptr ; The above statement WILL give you run-time error TRUE OR FALSE

True - Yes, ptr = 20 is illegal because you are assigning 20 (a address somewhere in the kernel ) to ptr. When you access the value *ptr, we don't have permission to access that space, resulting in segmentation fault.

steps to Dereference a pointer:

#include <stdio.h> int main() { int x=9; // First, we declare the integer variable to which the pointer points int *ptr; //Now, we declare the integer pointer variable. ptr=&x; // After the declaration of an integer pointer variable, we store the address of 'x' variable to the pointer variable 'ptr'. *ptr=8;// We can change the value of 'x' variable by dereferencing a pointer 'ptr' as given The above line changes the value of 'x' variable from 9 to 8 because 'ptr' points to the 'x' location and dereferencing of 'ptr', i.e., *ptr=8 will update the value of x. printf("value of x is : %d", x); return 0;}

int count = 10; int *extraPtr1 = ________ count ; Fill in the blank

&

logical operators

&& [and] || [or] ! [not]

int data [ ] = { 8, 6, 4, 2 } ; How would you print the address of the first cell ? printf ( "%p", __________ ) ;

&data [ 0 ]

When you write function, you have to ask :

--What kind of variables should be function take as input - pass by value --what kind of variables should this function take as output/input - pass by reference What kind of values should this function return: int, float, char, double , short, ... what kind of address should this function return : char *, int *, float *, ... Why do functions define const in front of the variables

Pay attention to = vs == operator ! Pay attention to || vs && operaot ! What is printed ? int x = 20 , y = 35 , z = 0; if ( ( x == 30 ) && ( y = 40 ) ) z = 10; printf ( "%d \n", z ) ;

0

Pay attention to = vs == operator ! Pay attention to || vs && operaot ! What is printed ? int x = 20 , y = 35 , z = 0; if ( ( x == 30 ) || ( y = 0 ) ) z = 10; printf ( "%d \n", z ) ;

0

int i = 0; int x = 0; for ( ; i < x ; i ++ ) i+=10 ; printf ( "i=%d \n", i ); what does it print

0

int data [ 4 ] = { 3, 4 } ; The first cells gets 3 The second cell gets 4 The third cell gets ____ and the fourth cells gets ___

0 and 0

int i = 0 ; do { i++ ; break; i = 20 ; } while ( i < 10 ) ; printf ( " %d \n", i ) ; The value of i printed here is :

1

char data [ 2 ] [ 3 ] = { { 2, 3 , 0} , { 1 , 4, 5 } } ; int i ; for ( i = 0 ; i < 3 ; i++ )printf ( "%d ", data [ 1 ] [ i ] ); The above code would print

1 4 5

Pay attention to = vs == operator ! Pay attention to || vs && operaot ! What is printed ? int x = 20 , y = 35 , z = 0; if ( ( x == 30 ) || ( y = 40 ) ) z = 10; printf ( "%d \n", z ) ;

10

What is the value of x printed in main assume header files are included and no syntax errors int x = 30 ; void changeX ( int x ) { x = 20 ; } main ( ) { int x = 10 ; changeX ( x ) ; printf ( " x %d \n", x ) ; }

10

int i = 0 ; do { i++ ; continue ; i = 20 ; } while ( i < 10 ) ; printf ( " %d \n ", i ) ; The value of i printed here is :

10

pay attention to conditions with just one variable. non-zero value of the condition is true, zero value is false. What is printed ? int x = 0 , y = 10; while ( x ) { y++ ; } printf ( "%d \n", y) ;

10

int i = 0; int x = 1; for ( ; i < x ; i ++ ) i+=10 ; printf ( "i=%d \n", i ); what does it print

11

int i = 0; do { i++ ; i = 12; break ; } while ( i < 10 ); printf ( "%d \n", i); The value of i printed here is :

12

int i = 0; do { i++ ; i = 12; continue; } while ( i < 10 ); printf ( "%d \n", i); The value of i printed here is :

12

what is the size (in terms of bytes) of this array char data [ 12 ] ;

12

Pay attention to variables declared inside the { }. They are not visible outside What is printed ? int x = 10, y = 15; for ( x = 10 ; x < 20 ; x++ ) { int y = 20; break; }

15

int data [ ] = { 8, 6, 4, 2 } ; int *ptr1 = data ; ptr1++ ; ptr1++ ; int *ptr2 ; ptr2 = ptr1 ; ptr2++ ; printf ( "%d\n", *ptr2) ; what is printed ?

2

What is the value of x printed in main assume header files are included. int x = 30 ; void changeX ( int *x ) { *x = 20 ; } main ( ) { int x = 10 ; changeX ( &x ) ; printf ( " x %d \n", x ) ; }

20

What is the value of x printed in main assume header files are included. int x = 30 ; void changeX ( int x ) { x = 20 ; } main ( ) { changeX ( x ) ; printf ( " x %d \n", x ) ; }

30

int data [ ] = { 8, 6, 4, 2 } ; int *ptr1 = data ; ptr1++ ; ptr1++ ; int *ptr2 ; ptr2 = ptr1 ; printf ( "%d\n", *ptr2) ; what is printed ?

4

What is the size of this array int data [ 4 ] = { 4, 4 } ; ?

4 * 4 = 16 bytes

int x = 30; void changeX(int *y) { *y = x + 20; } int main(){ // int x = 10; changeX(&x); printf(" x %d \n", x); return (0); } what is the value of x printed ?

50

int data [ ] = { 8, 6, 4, 2 } ; int * ptr ; ptr = data ; ptr++ ; printf ( "%d\n", *ptr); what is printed ?

6

int data [ ] = { 8, 6, 4, 2 } ; int * ptr ; ptr = data ; printf ( "%d\n", *ptr); What is printed ?

8

statement 1: for ( i = 0 ; i < 10 ; i++ ) printf ( "\n"); statement 2: for ( i = 0 ; i < 10 ; ) printf ( "\n"); i++ ; Both statements produce same result . True or False

False- the second statement would produce infinite loop

#include <stdio.h> int main ( ) { int x = 10; switch x { case 10 : printf ( "Yes" ); break ; } } // The above code compiles fine. True or False

False- Yes, there should be ( ) around x in the switch statement

Let us try this one more time.. int data [ 3 ] = { 2, 3, 4 }; int * ptr ; data = ptr ; The above statement is valid . True or False

False. Yes, you cannot assign a value to a pointer constant.

int i = 0 ; int x = 10 ; switch ( 0 ) { case 0: printf ( "Hello World"); break; case 1 : printf ( "Hi World"); break; case 10 : printf ( "Good Bye World"); break; } ; What does it print ?

Hello World

Cells in an array are contiguous

True

Pay attention to = vs == operator ! Pay attention to || vs && operaot ! What is printed ? int x = 10; if (( x == 10 ) || ( x = 0 ) ) printf ( " True \n"); else printf ( "False \n");

True

What is wrong with this program ? if there is no problem, what is printed ? int x = 11, y = 20 ; if ( x < 10 ) printf ( " %d \n", x) ; else ( x > 10 ) printf ( "%d \n", y );

compilation error, keyword "if" is missing after else

How to declare an array-1D?

dataType arrayName[arraySize];

int i = 0; int x = 1; for ( ; i < x ; break ) i+=10 ; printf ( "i=%d \n", i ); The above program clip will not compile. True

i=12

How to define a pointer variable?

int *ptr ; ptr is now a pointer variable

What is the proper way to print the address of a variable int x = 20 ;

printf ( " %p \n", &x ) ;

The printf function is defined as:

printf ( "format specifier", var1, var2, ... ) ;

int i = 0; do { i++ ; i = 12; break; } while ( 1 ) ; printf ( "i= \n", i ) ; What does this do ?

prints i = 12

&& (AND) Operator

returns TRUE when ALL the conditions under consideration are true and returns FALSE when any ONE or MORE than one condition is false.

The || (Or) operator

returns TRUE when ONE or more conditions are true, and FALSE when all conditions are false.

! operator

returns TRUE when condition is FALSE and returns FALSE when condition is TRUE. So the opposite of the condition.

int data [ 12 ] ; int x = _______ ( data ) ; which operator should we be using to get the number of bytes allocated to the array ? Fill in the blank

sizeof

// Jack wrote a function to increment the value of x. But he is using a pointer as the parameter // Which statement inside the function will do the job of incrementing . int func ( int *x ) { // which is statement is valid ? // statement 1 : *x++ ; // statement 2 * ( x++ ) ; // statement 3 ( *x ) ++ ; } main ( ) { int x = 10 ; func ( &x ) ; printf ( " x = %d \n " x ); }

statement 3

int i = 0 ; int x = 0 ; switch ( i ) { case 0: printf ( "Hello World"); break; case 1 : printf ( "Hi World"); break; case x : printf ( "Good Bye World"); break; } ; What does it print ?

syntax error, won't compile

What is wrong with this program ? int x = 20, y = 10 ; switch ( x ) { case x : printf ( "%d \n", x ) ; break; case y: printf ( "%d \n", y); break; // default : // break; }

you can't have expressions in case statements (marked red)


Kaugnay na mga set ng pag-aaral

Chapter 33: Management of Patients With Nonmalignant Hematologic Disorders course point questions

View Set

(Unit 9) UAS Rules and Regulations Progress check

View Set

GLB Capstone Midterm (Chapter 3)

View Set

CFA Foundations Exam Ch. 1 - Ch. 20

View Set

Kinesiology topic The Shoulder and Arm "exam coach 4/26/21"

View Set

Chapter 6: Consumer Markets and Consumer Buying Behavior

View Set

Business 1.0-Chapter 17-Understanding Accounting and Financial Information

View Set