Languages-C & C Plus Plus [TCS Placement]: Sample Questions 49 - 50 of 119

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 49

Describe in Detail Essay▾

Is it possible to create your own header files?

Edit

Explanation

  • Yes, it is possible to create a customized header file which include function prototypes used in the program
  • #include directive followed by the name of your header file will be used in the source.
  • How to create own header files in c
  1. Open notepad and write the function used in the program. An example is shown below.
    1. int add(int a,int b)
    2. {
    3. return(a+b);
    4. }
  2. Now save the notepad file with . h extension.
  3. In the “c” source file include the header file
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<add.h> //header file created
  4. void main()
  5. {
  6. int a,b,s;
  7. clrscr();
  8. printf(“Enter the value of a and b:”);
  9. scanf(“%d%d”,&a,&b);
  10. s=add(a,b);
  11. printf(“Add=%d”,s);
  12. getch();
  13. }

Question 50

Question MCQ▾

Which of the following is correct about class and structure?

Choices

Choice (4)

a.

Pointer to structure or classes cannot be declared.

b.

Class data members are private by default while that of structure are public by default.

c.

Class data members are public by default while that of structure are private.

d.

Class can have member functions while structure cannot.

Edit

Answer

b.

Explanation

  • Structure members are public by default and hence can be accessed anywhere in the program through structure variable.
  • Class members, on other hand are private to provide security and code encapsulation.
  • C ++ , structures can also contain member functions, just like in Class- but semantics are same as that of C structures.
  • Here՚s an example:
  1. #include<stdio.h>
  2. struct mystruct
  3. {
  4. int a ;
  5. void display(void)
  6. {
  7. printf("Hello world...[%d]",a);
  8. }
  9. } s;
  10. int main()
  11. {
  12. s.a=10;
  13. s.display();
  14. return 0;
  15. }
  • Output:

Hello world … [10]