Skip to main content

Important Introduction to Programming Short Questions

Chapter: 11
Question 1:
How do you calculate the sum of squares of an array's elements?
Answer: You can calculate the sum of squares of an array’s elements using a loop: sumOfSquares += arr[i] * arr[i];.
Chapter: 11
Question 2:
What is a linear search in the context of arrays?
Answer: A linear search is a method of finding a specific value in an array by checking each element one by one until the value is found or the end of the array is reached.
Chapter: 11
Question 3:
What is the purpose of the rand() function in C?
Answer: The rand() function generates a random integer between 0 and 32767.
Chapter: 11
Question 4:
How do you generate a random number between 1 and 6 in C?
Answer: You generate a random number between 1 and 6 using 1 + rand() % 6;.
Chapter: 11
Question 5:
How does the for loop help in array manipulation?
Answer: The for loop helps in array manipulation by iterating through each element of the array, allowing you to perform operations like initialization, summation, or copying.
Chapter: 11
Question 6:
Why is the index of the last element of an array always one less than its size?
Answer: The index of the last element is one less than the size because array indexing starts from 0.
Chapter: 11
Question 7:
How would you initialize an array of size 100 with all elements set to zero?
Answer: You can initialize an array of size 100 with all elements set to zero using: for (i = 0; i < 100; i++) arr[i] = 0;.
Chapter: 11
Question 8:
What does the following code do: int c[100]; do {cin >> z; c[i++] = z;} while(z != -1 && i < 100);?
Answer: This code reads integers from the user into the array c, stopping when the user enters -1 or 100 numbers have been entered.
Chapter: 11
Question 9:
What is the significance of break in a loop during a linear search?
Answer: The break statement exits the loop immediately when the desired value is found, improving efficiency by avoiding unnecessary comparisons.
Chapter: 11
Question 10:
How does the modulus operator help in generating random numbers within a specific range?
Answer: The modulus operator returns the remainder after division, which helps in limiting the range of random numbers, e.g., rand() % 6 gives a number between 0 and 5.