Thứ Hai, 11 tháng 6, 2012

Trắc nghiệm kiến thức cơ bản về C/C++

Trong bài này, chúng ta sẽ đến với 40 câu trắc nghiệm về ngôn ngữ lập trình C/C++. Với mỗi lựa chọn đúng, các bạn hãy giải thích tại sao lại lựa chọn đáp án đó.

C Language

1. Which symbol is used to make comments ?

a. //
b. #
c. !!
d. <!--

2. How would you insert pre-written code into a current program ?

a. #read <file>
b. #get <file>
c. #include <file>
d. #pre <file>

3. Which symbols represent a block of code ?

a. { ... code here }
b. ( ... code here )
c. [ ... code here ]
d. < ... code here >

4. In which standard library file is the function printf() located ?

a. stdlib.h
b. stdio.h
c. stdout.h
d. stdoutput.h

5. In order to properly use a variable...

a. The variable must have a valid type.
b. The variable name can not be a keyword (part of the C syntax).
c. The variable name must begin with a letter.
d. All of the above.

6. Which mathematical operators are in the correct order ?

a. Addition, Division, Modulus
b. Division, Multipication, Modulus
c. Modulus, Multipication, Subtraction
d. Modulus, Addition, Division

7. Which of the following are NOT relational operators ?

a. >
b. <
c. ==
d. >=

8. The first expression in a for loop is

a. The test expression.
b. The step value of the loop.
c. The value of our counter variable.
d. None of the above.

9. What is the break statement used for ?

a. To quit the program.
b. To quit the current iteration.
c. To stop the current iteration and begin the next iteration.
d. None of the above.

10. What is the continue statement used for ?

a. To continue to the next line of code.
b. To stop the current iteration and begin the next iteration from the beginning.
c. As an alternative to the else statement.
d. None of the above.

11. A function prototypes are useful 

a. Because they tell the compiler that a function is declared later.
b. Because they make the program more readable.
c. Because they allow the programmer to see a quick list of functions in the program along with the arguments for each function.
d. All of the above.

12. Because of variable scope

a. Variables created in a function cannot be used another function.
b. Variables created in a function can be used in another function.
c. Variables created in a function can only be used in the main function
d. None of the above.

13. Which symbol is used to declare a pointer ?

a. &
b. @
c. *
d. $

14. Which symbol is used to reference a pointer ?

a. *
b. &
c. @
d. $

15. Adding to a pointer that points to an array will

a. Cause an error.
b. Increase the value of the element that the pointer is pointing to.
c. Cause the pointer to point to the next element in the array.
d. None of the above.

16. To access the members of a structure, which symbol is used ?

a. *
b. -
c. ,
d. .

17. A member is a

a. Variable in a structure.
b. Structure's datatype.
c. Pointer to a structure.
d. None of the above.

18. Structures can...

a. Hold many variables of different types.
b. Have pointers to structures.
c. Be assigned to one another, given they are the same type.
d. All of the above.

19. In a C program, the first statement that will be executed is:

a. The first executable statement of the program.
b. The first executable statement of the main() function.
c. The first executable statment after the comment /*start here*/
d. The first executable statement of the end function.

20. The statement: int *jack

a. Declares that all variables ending with jack are ints.
b. Is a pointer declaration. jack is a pointer to an int variable.
c. Declares that jack is the address of a variable and that the address is an int.
d. Declares that all variables starting with jack are ints.

C++ Language

1. What function initalizes variables in a class:

a. Constructor
b. Destructor
c. Constitutor
d. A and B are correct.

2. To include code from the library in the program, such as iostream, a directive would be called up using this command.

a. #include <>; with iostream.h inside the brackets
b. include (iostreamh)
c. #include <> with iostream.h inside the brackets
d. include #iostream,h;

3. Single line comments explaining code would be preceded like in the following example

a. /**
b. //
c. *//
d. /*

4. Which line has all reserved words ?

a. char, int, float, doubled, short, long, unsigned, signed
b. sizeof, const, typedef, static, voided, enum, struct, union
c. if, else, for, while do, switch, continue, break
d. defaulted, goto, return, extern, private, public, protected

5. What punctuation must each command line have at the end of the line ?

a. :
b. ,
c. !
d. ;

6. The C++ language is

a. case-sensitive.
b. Not case-sensitive.
c. It depends
d. None of these

7. The number 5.9875e17 must be stored in a(n):

a. int
b. long
c. double
d. float

8. Select the correct definition for a string variable.

a. string mystr;
b. string mystr[20];
c. string[20] mystr;
d. char mystr[20];

9. The sentence "Hello world!" uses _____ elements in a character array.

a. 10
b. 11
c. 12
d. 13

10. When you are creating a structure, you need to use the following keyword

a. structure
b. struct
c. object
d. record

11. Select the correct function definition (NOT prototype) from the list below.

a. void intro();
b. double sin(double rad);
c. int foo(int bar; double baz)
d. double pow(double num, int pow);

12. Cout can print multiple values or variables in a single command using the following syntax:

a. cout << "Hi" + bob + "\n";
b. cout << "Hi" << bob << "\n";
c. cout << "Hi", bob, "\n";
d. cout << ("Hi" & bob & "\n");

13. Write a for loop that counts from 0 to 5.

a. for (c = 0; c <= 5; c++)
b. for (int c = 0; c <= 6; c++)
c. for (c = 0; c < 5; c++)
d. for (c = 0; c < 5; c++);

14. What does the statement #include do ?

a. It defines the function iostream.h
b. It tells the compiler where the program begins
c. It defines the words TRUE and FALSE
d. It allows the programmer to use cout << It defines the statement return

15. Which of the following converts an integer "value" to its ASCII equivalent ?

a. atoi(value)
b. cout << value
c. (char) value
d. char (value)

16. Indicate which data type is not part of standard C++.

a. bool
b. int
c. real
d. double

17. Which one of the choices would produce the following output ?

a. cout << "Hello" << "World";
b. cout << "Hello \n World";
c. cout << "Hello World\n";
d. cin >> "Hello World";

18. What is the output of the following code?

for (int i=0; i<10; i++); cout << i%2 << " "; }

a. 0 1 2 3 4 5 6 7 8 9
b. 0 2 4 6 8 10 12 14 16 18
c. 1 0 1 0 1 0 1 0 1 0
d. 0 1 0 1 0 1 0 1 0 1

19. What is the output of the following code?

for (int a = 1; a <= 1; a++) cout << a++; cout << a;

a. 22
b. 12
c. error
d. 23

20. For which values of the integer _value will the following code become an infinite loop?

int number=1;
while (true) {
cout << number;
if (number == 3) break;
number += _value; }

a. only 0
b. only 1
c. only 2
d. only 1 or 2


Editor: Dương T.K. Linh, Vương L. Kiều (Baomathethong.blogspot.com)
Copyrighted Content: N/A

Không có nhận xét nào:

Đăng nhận xét