Random Post

Sunday, October 9, 2011

C and C++ Questions and their Answers

                          C AND C++ QUESTIONS WITH ANSWERS


1. What is the correct value to return to the operating system upon the successful completion of a program?
A. -1
B. 1
C. 0
D. Programs do not return a value.
Ans: C

2. What is the only function all C++ programs must contain? A. start()
B. system()
C. main()
D. program()
Ans: C

3. What punctuation is used to signal the beginning and end of code blocks?
A. { }
B. -> and <-
C. BEGIN and END
D. ( and )
Ans: A

4. What punctuation ends most lines of C++ code? A. .
B. ;
C. :
D. '
Ans: B

5. Which of the following is a correct comment?
A. */ Comments */
B. ** Comment **
C. /* Comment */
D. { Comment }
Ans: C

6. Which of the following is not a correct variable type? A. float
B. real
C. int
D. double
Ans: B

7. Which of the following is the correct operator to compare two variables?
A. :=
B. =
C. equal
D. ==
Ans: D

IF STATEMENTS


8. Which of the following is true? A. 1
B. 66
C. .1
D. -1
E. All of the above
Ans: E

9. Which of the following is the boolean operator for logical-and? A. &
B. &&
C. |
D. |&
Ans: B

10. Evaluate !(1 && !(0 || 1)). A. True
B. False
C. Unevaluatable
Ans: A
11. Which of the following shows the correct syntax for an if statement? A. if expression
B. if { expression
C. if ( expression )
D. expression if
Ans: C

LOOP


12. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run? A. 10
B. 9
C. 0
D. 1
Ans: A

13. When does the code block following while(x<100) execute? A. When x is less than one hundred
B. When x is greater than one hundred
C. When x is equal to one hundred
D. While it wishes
Ans: A
14. Which
 is not a loop structure? A. For
B. Do while
C. While
D. Repeat Until
Ans: D

15. How many times is a do while loop guaranteed to loop? A. 0
B. Infinitely
C. 1
D. Variable
Ans: C

FUNCTIONS

16. Which is not a proper prototype? A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();
Ans: B
17. What is the return type of the function with prototype: "int func(char x, float v, double t);"
A. char
B. int
C. float
D. double
Ans: B

18. Which of the following is a valid function call (assuming the function exists)? A. funct;
B. funct x, y;
C. funct();
D. int funct();
ANS: C

19. Which of the following is a complete function? A. int funct();
B. int funct(int x) {return x=x+1;}
C. void funct(int) {cout<<"Hello"}
D. void funct(x) {cout<<"Hello"}
Ans: B

20. Which follows the case statement? A. :
B. ;
C. -
D. A newline
Ans: A

21. What is required to avoid falling through from one case to the next? A. end;
B. break;
C. Stop;
D. A semicolon.
Ans: B

22. What keyword covers unhandled possibilities? A. all
B. contingency
C. default
D. other
Ans: C

23. What is the result of the following code? int x=0;
switch(x)
{
  case 1: cout<<"One";
  case 0: cout<<"Zero";
  case 2: cout<<"Hello World";
}
A. One
B. Zero
C. Hello World
D. ZeroHello World
Ans: D

POINTERS


24. Which of the following is the proper declaration of a pointer? A. int x;
B. int &x;
C. ptr x;
D. int *x;
Ans: D
25. Which of the following gives the memory address of integer variable a? A. *a;
B. a;
C. &a;
D. address(a);
Ans: C
26. Which of the following gives the memory address of a pointer a? A. a;
B. *a;
C. &a;
D. address(a);
Ans: A
27. Which of the following gives the value stored at the address pointed to by the pointer a?
A. a;
B. val(a);
C. *a;
D. &a;
Ans: C
28. Which of the following is the proper keyword to allocate memory?
A. new
B. malloc
C. create
D. value
Ans: A
29. Which of the following is the proper keyword to deallocate memory?
A. free
B. delete
C. clear
D. remove
Ans: B
STRUCTURES
30. Which of the following accesses a variable in structure b?
A. b->var;
B. b.var;
C. b-var;
D. b>var;
Ans: B
31. Which of the following accesses a variable in structure *b?
A. b->var;
B. b.var;
C. b-var;
D. b>var;
Ans: A
32. Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a;
D. struct a_struct {int a;};
D
33. Which properly declares a variable of struct foo?
A. struct foo;
B. foo var;
C. foo;
D. int foo;
Ans: B
ARRAYS
34. Which of the following correctly declares an array?
A. int anarray[10];
B. int anarray;
C. anarray{10};
D. array anarray[10];
Ans: A
35. What is the index number of the last element of an array with 29 elements? A. 29
B. 28
C. 0
D. Programmer-defined
Ans: B
36. Which of the following is a two-dimensional array?
A. array anarray[20][20];
B. int anarray[20][20];
C. int array[20, 20];
D. char array[20];
Ans: B
37. Which of the following correctly accesses the seventh element stored in foo, an array with 100 elements?
A. foo[6];
B. foo[7];
C. foo(7);
D. foo;
Ans: A
38. Which of the following gives the memory address of the first element in array foo, an array with 100 elements?
A. foo[0];
B. foo;
C. &foo;
D. foo[1];
Ans: B
STRINGS
39. Which of the following is a static string?
A. Static String
B. "Static String"
C. 'Static String'
D. char string[100];
Ans: B
40. What character ends all strings?
A. '.'
B. ' '
C. '\0'
D. '\n'
Ans: C
41. Which of the following reads in a string named x with one hundred characters? A. cin.getline(x, 100, '\n');
B. cin.getline(100, x, '\n');
C. readline(x, 100, '\n');
D. read(x);
42. Which of the following functions compares two strings?
A. compare();
B. stringcompare();
C. cmp();
D. strcmp();
Ans: D
43. Which of the following adds one string to the end of another?
A. append();
B. stringadd();
C. strcat();
D. stradd();
Ans: C

FILE I/O
44. Which of the following classes handlers file input?
A. ofstream
B. ifstream
C. instream
D. inputfile
Ans: B
45. Which of the following is not a valid ofstream argument?
A. ios::app
B. ios::trunc
C. ios::noreplace
D. ios::create
Ans: D
46. What does ios::ate mean as an argument to ofstream?
A. Open file, but do not create.
B. Open file, create.
C. Open file for read access only.
D. Open file, set the position to the end.
Ans: D
47. How would you output to an open file named a_file?
A. a_file.out("Output");
B. a_file="Output";
C. a_file<<"Output";
D. a_file.printf("Output");
Ans: C
48. What header file contains C++ file I/O instructions?
A. iostream.h
B. fstream.h
C. infstream.h
D. outstream.h
Ans: B
TYPECASTING
49. Which header file do you need to include to use typecasting?
A. iostream.h
B. ctype.h
C. math.h
D. None
Ans: D
50. Which is a valid typecast?
A. a(char);
B. char:a;
C. (char)a;
D. to(char, a);
Ans: C
51. Why can typecasting be dangerous?
A. Some conversions are not defined, such as char to int.
B. You might permanently change the value of the variable.
C. You might temporarily lose part of the data - such as truncating a float when typecasting to an int.
D. There are no dangers.
Ans: C

52. Which is a good use for typecasting?
A. To allow division of two integers to return a decimal value.
B. To allow your program to use nothing but integers.
C. To change the return type of a function.
D. To swap variables rapidly.
Ans: A

53. Which conversion is not possible? A. int to float
B. float to int
C. char to float
D. All are possible
Ans: D

C++ CLASSES


54. What purpose do classes serve?
A. data encapsulation
B. providing a convenient way of modeling real-world objects
C. simplifying code reuse
D. all of the above
Ans: D

55. Which is not a protection level provided by classes in C++?
A. protected
B. hidden
C. private
D. public
Ans: B

56. What value must a destructor return? A. A pointer to the class.
B. An object of the class.
C. A status code determining whether the class was destructed correctly
D. Destructors do not return a value.
Ans: D

57. Which of the following is a valid class declaration? A. class A { int x; };
B. class B { }
C. public class A { }
D. object A { int x; };
Ans: A

58. Which functions will every class contain?
A. None
B. Constructor
C. Destructor
D. Both a constructor and a destructor
Ans: D

INLINE FUNCTIONS IN C++


59. What does the inline keyword do? A. Indicates a function declaration
B. Tells the compiler to use the function only within the same source code file
C. Causes all function calls to be replaced by the code from the function
D. Allows one-line function declarations
Ans: C

60. Why would you want to use inline functions? A. To decrease the size of the resulting program
B. To increase the speed of the resulting program
C. To simplify the source code file
D. To remove unnecessary functions
Ans: B

61. Which of the following is a limit on inline functions? A. Inline functions cannot return a value.
B. Inline functions must resturn a value.
C. Inline functions must be less than ten lines.
D. The compiler may choose to ignore an inline directive.
Ans: D

62. Which of the following is a valid inline for function foo? A. inline void foo() {}
B. void foo() inline {}
C. inline:void foo() {}
D. None of the above
Ans: A

63. How can you ensure that an inline function isn't inlined for a particular function call for function foo?
A. unline x();
B. noexpand x();
C. x();
D. This is not possible on a case-by-case basis
Ans: D

COMMAND LINE ARGUMENTS C++


64. What variables stores the number of arguments to a program?
A. argc
B. argv
C count
D. arglen
Ans: A

65. What is argv[0]? A. The number of arguments to the program
B. The name of the program
C. The first argument to the program
D. This syntax is illegal
Ans: B
66. What type is argv? A. char *
B. int
C. char **
D. It's not a variable
Ans: C

67. In what order do the two command line variables appear in the definition of main?
A. Count then argument array
B. Argument array then count
C. They don't appear in the definition of main
D. There is only one argument.
Ans: A

68. What does the argument count variable store?
A. the number of arguments
B. the number of arguments plus one
C. the number of arguments minus one
D. The total size of the argument array
Ans: B

No comments:

Post a Comment