Lesson1 Flashcards
What is an array in C?
A collection of similar type of data items stored at contiguous memory locations
How do you declare an array in C?
data_type array_name[array_size];
What is the syntax for initializing an array during declaration?
data_type array_name[size] = {value1, value2, …, valueN};
What is the starting index for arrays in C?
0
What function is used to find the length of a string in C?
strlen
What is the purpose of pointers in C?
To store the address of another variable
True or False: An array can only store primitive data types.
True
Fill in the blank: An array is the simplest _______ structure in C.
data
What is the syntax for accessing an element in an array?
array_name[index];
What are the two types of arrays mentioned?
- One-dimensional arrays
- Two-dimensional arrays
How do you initialize an array using index?
By assigning values to each index individually, e.g., Arr[0]=value;
What is a two-dimensional array?
An array of one-dimensional arrays, visualized as a table with rows and columns
What is the output of accessing arr[1][0] in a 2D array defined as int arr[2][3] = { {1, 4, 2}, {3, 6, 8} };?
3
What is the purpose of the ‘sizeof’ operator in the context of arrays?
To determine the size of the array in bytes
What does the term ‘self-referential structure’ refer to?
A structure that contains a pointer to its own type
True or False: You can use pointers to access elements of an array.
True
How do you read values into an array in C?
Using a loop and scanf to input each value
What is the output of the following code snippet? int marks[5] = {80, 60, 70, 85, 75}; printf(‘%d’, marks[2]);
70
How do you find the sum of elements in an array?
Using a loop to iterate through the array and accumulate the sum
What is the formula to calculate the average of an array?
average = sum / number_of_elements
What is the syntax for defining a structure in C?
struct structure_name { data_type member1; data_type member2; … };
What is an example of a basic string function in C?
- strcat
- strcpy
- strstr
Fill in the blank: A _______ can store multiple values in a single variable.
array
What does ‘union’ in C allow you to do?
Store different data types in the same memory location
What is the maximum number of dimensions an array can have in C?
Theoretically, it can be up to 255 dimensions
What does the term ‘enumeration data type’ refer to?
A user-defined type consisting of a set of named integer constants
How is memory allocated for an array in C?
The compiler allocates a block of memory based on the array’s size upon declaration
True or False: Arrays in C can only hold integers.
False
What is the output of the following code snippet? printf(‘%d’, marks[5]); where marks is defined as int marks[5];
Undefined behavior (out of bounds access)
How do you sort an array in C?
Using a sorting algorithm like bubble sort, quicksort, etc.
What are the two main operations performed on arrays?
- Accessing elements
- Modifying elements
What is the significance of the index in an array?
It determines the position of an element within the array
What is the syntax to declare a two-dimensional array?
data_type array_name[rows][columns];
What is a string in C?
A one-dimensional array of characters terminated by a null character (‘\0’)
The null character is crucial for identifying the end of the string.
What differentiates a character array from a C string?
A C string is terminated with a unique character ‘\0’
This termination character allows the program to determine where the string ends.
What is the syntax for declaring a C string?
char string_name[size];
How is a string literal declared in C?
char ch2[11] = ‘javatpoint’;
The compiler automatically adds the ‘\0’ character.
What does the strlen() function do?
Calculates the length of a given string excluding the null character.
What is the purpose of the strcpy() function?
Copies the contents of the source string to the destination string.
What does the strcat() function do?
Concatenates or joins the first string with the second string.
What does the strcmp() function return?
Returns 0 if both strings are equal.
What is the structure in C?
A user-defined data type that groups related variables of possibly different types into a single type.
What keyword is used to define a structure in C?
struct
What is the difference between an array and a structure?
An array is a homogeneous collection of similar types, while a structure can have elements of different types.
What does the following code do? char arr[3][10] = {“Geek”, “Geeks”, “Geekfor”};
Defines an array of strings, where each string can hold up to 9 characters plus the null terminator.
What is the output of the printf statement printing a string array?
Prints each string stored in the array, one per line.
Fill in the blank: In C, an array of strings is a _______ array of character types.
two-dimensional
What is the significance of the null character (‘\0’) in strings?
Indicates the termination of the string.
How do you read a string input from the keyboard in C?
Using scanf(“%s”, str);
What will the following code print? char str[] = “GeeksforGeeks”; length = strlen(str);
Length of the string, which is 13.
True or False: The strcpy() function can copy one string to another of different sizes.
False
The destination must be large enough to hold the copied string.
What is the output of the following code? printf(“%d\n”, strcmp(“Hello”, “Hello”));
0
What is the purpose of the printf function in C?
To output formatted text to the console.
What is the maximum number of string values that can be stored in a string array declared as char variable_name[r][m]?
r
What is the maximum number of character values that can be stored in each string array declared as char variable_name[r][m]?
m
What is the main difference between an array and a structure in C?
An array is a homogenous collection of similar types, while a structure can have elements of different types.
How do you declare a structure in C?
Using the struct keyword followed by the structure name and member variables with their data types.
What is the syntax for declaring a structure in C?
struct structure_name { data_type member_name1; data_type member_name2; … };
What is a structure template in C?
The syntax used to declare a structure without allocating memory.
Provide an example of a structure declaration for a Book.
struct book { char title[50]; char author[50]; double price; int pages; };
How do you create an instance of a structure in C?
By defining variables of the structure type.
What are the two methods to define structure variables?
- Structure Variable Declaration with Structure * Structure Variable Declaration after Structure definition
How do you access members of a structure?
Using the dot operator (.) or the structure pointer operator (->).
What is the output of printf when accessing structure members?
It displays the values of the structure’s members.
What is an array of structures in C?
A collection of multiple structure variables where each variable contains information about different entities.
How do you define an array of structures?
struct book b[3] = { {“Learn C”, 650.50, 325}, {“C Pointers”, 175, 225}, {“C Pearls”, 250, 250} };
What is a union in C?
A user-defined data type that can contain elements of different data types, but only one member can store data at a time.
What is the main advantage of using unions?
Unions provide an efficient way of using the same memory location for multiple purposes.
What is the syntax for defining a union?
union union_name { datatype member1; datatype member2; … };
How do you initialize a union member?
By assigning a value to the member directly after declaring a union variable.
What is a pointer in C?
A derived data type that can store the address of other C variables or a memory location.
What is the syntax for declaring a pointer?
datatype *ptr;
How do you assign an address to a pointer?
ptr = &var; where var is a variable.
What operator is used to dereference a pointer?
The asterisk (*) operator.
What happens when you change the value of a pointer’s location?
It updates the value of the variable that the pointer is pointing to.
What is the output of a program that prints the size of a structure?
It displays the total size in bytes of the structure.
Fill in the blank: An array of structures in C can be defined as the collection of multiple _______.
structure variables
What is the data type of the variable ‘x’ in the code?
int
What is the data type of the variable ‘y’ in the code?
float
What is the data type of the variable ‘z’ in the code?
char
How is the pointer ‘ptr_x’ initialized?
ptr_x = &x
What type of pointer is ‘ptr_y’?
float *
What does ‘ptr_z’ point to?
char
What is the output format specifier for an integer in printf?
%d
What is the output format specifier for a float in printf?
%f
What is the output format specifier for a char in printf?
%c
What is the output of ‘printf(“Value of x = %d\n”, *ptr_x);’
Value of x = 10
What is the output of ‘printf(“Value of y = %f\n”, *ptr_y);’
Value of y = 1.300000
What is the output of ‘printf(“Value of z = %c\n”, *ptr_z);’
Value of z = p
Fill in the blank: The pointer ‘ptr_x’ is of type _______.
int *
True or False: The value of ‘y’ is printed as 1.3.
False
What does the ‘*’ operator do when used with a pointer?
Dereferences the pointer