Topic: Searching | BPSC Paper-2 Computer Science Notes and MCQ

MCQ Searching

Topic: Searching 

(MCQ Qu. Ans Below Notes)↓

Summary notes on topic "Searching" based on 12th NCERT books

Searching means trying to locate a particular element called key in a collection of elements.

Search specifies whether that key is present in the collection or not. Also, if the key is present, it tells the position of that key in the given collection.

• Linear search checks the elements of a list, one at a time, without skipping any element. It is useful when we need to search for an item in a small unsorted list, but it is slow and time-consuming when the list contains a large number of items.

The time taken to search the list increases as the size of the list increases.

• Binary search takes a sorted/ordered list and divides it in the middle. It then compares the middle element with the key to be searched. If the middle element matches the key, the search is declared successful and the program ends. If the middle element is greater than the key, the search repeats only in the first half of the list.

If the middle element is lesser than the key, the search repeats only in the second half of the list.

This splitting and reduction in list size continue till the key is found or the remaining list consists of only one item.

• In binary search, comparisons that do not find the key still give us idea about the location where the key may probably be found! They reveal whether the key is before or after the current middle position in the list, and we can use this information to narrow down or reduce our searching efforts.

• Hash based searching requires only one key comparison to discover the presence or absence of a key, provided every element is present at its designated position decided by a hash function.

It calculates the position of the key in the list using a formula called the hash function and the key itself.

• When two elements map to the same slot in the hash table, it is called collision.

• The process of identifying a slot for the second and further items in the hash table in the event of collision, is called collision resolution.

• A perfect hash function maps every input key to a unique index in the hash table. If the hash function is perfect, collisions will never occur.

MCQ Qu-Ans

C++ MCQ Quiz

1. What is the difference between a compiler and an interpreter?
a) A compiler translates the entire program into machine code, while an interpreter translates it line by line.
b) A compiler translates the program line by line, while an interpreter translates the entire program into machine code.
c) A compiler and an interpreter are the same thing.
d) A compiler translates the program into assembly language, while an interpreter translates it into machine code.
e) A compiler translates the program into machine code, while an interpreter translates it into assembly language.

2. Which of the following is not a data type in C?
a) char
b) int
c) float
d) double
e) string

3. What is the purpose of a pointer in C?
a) To store the address of a variable.
b) To store the value of a variable.
c) To store the size of a variable.
d) To store the data type of a variable.
e) To store the name of a variable.

4. Which of the following is not a control structure in C?
a) if-else
b) for
c) while
d) switch
e) do-while

5. What is the output of the following code snippet?
```
int x = 5;
printf("%d\n", ++x);
```
a) 4
b) 5
c) 6
d) 10
e) None of the above

6. Which of the following is not a logical operator in C?
a) &&
b) ||
c) !
d) &
e) |

7. What is the purpose of the 'break' statement in C?
a) To terminate a loop or switch statement.
b) To skip the current iteration of a loop.
c) To execute the next iteration of a loop.
d) To return a value from a function.
e) To declare a variable.

8. What is the difference between '==' and '=' in C?
a) '==' is used for assignment, while '=' is used for comparison.
b) '==' is used for comparison, while '=' is used for assignment.
c) '==' and '=' are interchangeable.
d) '==' is used for addition, while '=' is used for subtraction.
e) '==' is used for multiplication, while '=' is used for division.

9. What is the output of the following code snippet?
```
int x = 10, y = 5;
if (x > y) {
printf("%d\n", x);
} else {
printf("%d\n", y);
}
```
a) 5
b) 10
c) 15
d) 0
e) None of the above

10. What is the purpose of the 'continue' statement in C?
a) To terminate a loop or switch statement.
b) To skip the current iteration of a loop.
c) To execute the next iteration of a loop.
d) To return a value from a function.
e) To declare a variable.

11. What is the output of the following code snippet?
```
int x = 5, y = 10;
printf("%d\n", x + y);
```
a) 5
b) 10
c) 15
d) 50
e) None of the above

12. What is the purpose of the 'sizeof' operator in C?
a) To return the value of a variable.
b) To return the size of a variable.
c) To return the data type of a variable.
d) To return the name of a variable.
e) To return the address of a variable.

13. What is the output of the following code snippet?
```
int x = 5, y = 10;
if (x > y) {
printf("%d\n", x);
} else if (y > x) {
printf("%d\n", y);
} else {
printf("Equal\n");
}
```
a) 5
b) 10
c) Equal
d) 15
e) None of the above

14. What is the purpose of the 'do-while' loop in C?
a) To execute a block of code while a condition is true.
b) To execute a block of code at least once, even if the condition is false.
c) To execute a block of code until a condition is true.
d) To execute a block of code a specific number of times.
e) To execute a block of code based on a switch statement.

15. What is the output of the following code snippet?
```
int x = 5, y = 10;
printf("%d\n", x * y);
```
a) 5
b) 10
c) 15
d) 50
e) None of the above

16. What is the purpose of the 'else' statement in C?
a) To execute a block of code if a condition is true.
b) To execute a block of code if a condition is false.
c) To execute a block of code based on a switch statement.
d) To execute a block of code a specific number of times.
e) To execute a block of code until a condition is true.

17. What is the output of the following code snippet?
```
int x = 5;
printf("%d\n", x++);
```
a) 4
b) 5
c) 6
d) 10
e) None of the above

18. What is the purpose of the 'switch' statement in C?
a) To execute a block of code if a condition is true.
b) To execute a block of code if a condition is false.
c) To execute a block of code based on a specific value.
d) To execute a block of code a specific number of times.
e) To execute a block of code until a condition is true.

19. What is the output of the following code snippet?
```
int x = 5, y = 10;
if (x > y) {
printf("%d\n", x);
} else if (y > x) {
printf("%d\n", y);
}
```
a) 5
b) 10
c) 15
d) 0
e) None of the above

20. What is the purpose of the 'for' loop in C?
a) To execute a block of code while a condition is true.
b) To execute a block of code at least once, even if the condition is false.
c) To execute a block of code until a condition is true.
d) To execute a block of code a specific number of times.
e) To execute a block of code based on a switch statement.

Post a Comment

Thanks for your best idea after moderation comment published here.

Previous Post Next Post