UGC NET · Computer Science

Programming in C/C++ Previous Year Questions

19 Programming in C/C++ (Computer Science) questions asked in UGC NET previous year papers, each with the correct answer and explanation. Read them free on UTpaper, or attempt the full papers as timed online tests.

  • 19 questions
  • Official answer key
  • Free

Other Computer Science topics

Q1UGC NET Computer Science, June 2014 (Paper II & III, Selected Questions)
while (87) printf ("computer"); The above C statement will
  1. Aprint "computer" 87 times
  2. Bprint "computer" 0 times
  3. Cprint "computer" 1 times
  4. Dprint "computer" infinite times
Show answerHide answer

Correct answer: (D) print "computer" infinite times

Explanation

Parameter of a while statement in a languages takes two values true in the form of non-zero and false in the form of zero. Here 87 is a non-zero value and hence the while block gets true for infinite times.

Q2UGC NET Computer Science, June 2014 (Paper II & III, Selected Questions)
The control string in C++ consists of three important classifications of characters
  1. AEscape sequence characters. Format specifies and White-space characters
  2. BSpecial characters, White-space characters and Non-white space characters
  3. CFormat specifies, White-space characters and Non-white space characters
  4. DSpecial characters, White-space characters and Format specifies
Show answerHide answer

Correct answer: (C) Format specifies, White-space characters and Non-white space characters

Explanation

In programming language we use unique keywords to store data, just like that, the control string is use to access the different type of keyword, and control strings are the characters that use with the % (percent) and it is a classification of format specifier, white space and non-white space characters.

Q3UGC NET Computer Science, December 2014 (Paper II & III, Selected Questions)
What will be the output of the following 'C' code? main ( ) { int x = 128 ; printf("\n%d", 1 + x++); }
  1. A128
  2. B129
  3. C130
  4. D131
Show answerHide answer

Correct answer: (B) 129

Explanation

x++ is a post-increment, so the old value 128 is used in the expression and printf prints 1 + 128 = 129; x becomes 129 only afterwards.

Q4UGC NET Computer Science, June 2015 (Paper II & III, Selected Questions)
What is the output of the following program? (Assume that the appropriate directive are included and there is no syntax error) main () { char S[ ] = "ABCDEFGH", printf ("%C", * (&S[3])); printf ("%s", S + 4); printf ("%u", S); /* Base address of S is 1000*/ }
  1. AABCDEFGH1000
  2. BCDEFGH1000
  3. CDDEFGHH1000
  4. DDEFGH1000
Show answerHide answer

Correct answer: (D) DEFGH1000

Explanation

S occupies addresses 1000 to 1008: A B C D E F G H \0. S[3] is 'D', and *(&S[3]) is 'D' itself, so %c prints D. S + 4 skips 4 characters, so %s prints "EFGH". %u prints the base address of S, 1000. Final output: DEFGH1000.

Q5UGC NET Computer Science, June 2015 (Paper II & III, Selected Questions)
When the inheritance is private, the private methods in base class are _______ in the derived class (in C++).
  1. AInaccessible
  2. BAccessible
  3. CProtected
  4. DPublic
Show answerHide answer

Correct answer: (A) Inaccessible

Explanation

Inaccessible in the derived class, only public or protected members are inherited So answer is (a)

Q6UGC NET Computer Science, June 2015 (Paper II & III, Selected Questions)
Which of the following in C++, is inherited in a derived class from base class?
  1. AConstructor
  2. BDestructor
  3. CData Members
  4. DVirtual Methods
Show answerHide answer

Correct answer: (C) Data Members

Explanation

Data members in C++ is inherited in a derived class from base class. Constructor is a member function of a class which initialize objects of a class. In C++, constructor is automatically called when object create. It is special member function of the class. Destructor is a member function which destructs or deletes an object. Virtual methods is a method which is redefined (over- ridden) in derived class.

Q7UGC NET Computer Science, June 2015 (Paper II & III, Selected Questions)
Which of the following is incorrect in C++?
  1. AWhen we write overloaded function we must code the function for each usage
  2. BWhen we write function template we code the function only once
  3. CIt is difficult to debug macros
  4. DTemplates are more efficient than macros
Show answerHide answer

Correct answer: (D) Templates are more efficient than macros

Explanation

The first three statements are correct; the fourth is incorrect. Templates are more powerful and obey C++ syntactic rules, whereas macros do not, but templates take more compilation time and code size, so templates are not more efficient than macros.

Q8UGC NET Computer Science, December 2015 (Paper II & III, Selected Questions)
Consider the following program : #include < stdio.h> main ( ) { int i, inp; float x, term = 1, sum = 0 scanff ("% f", & inp, &x); for(i = 1, < = inp; i ++) { term = term * x/t; sum = sum + term; } printf("Result = %f\n", sum); } The program computes the sum of which of the following series?
  1. Ax + x²/2 + x³/3 + x⁴/4 +….
  2. Bx + x²/2! + x³/3! + x⁴/4! +….
  3. C1 + x²/2 + x³/3 + x⁴/4 +….
  4. D1 + x²/2! + x³/3! + x⁴/4! +….
Show answerHide answer

Correct answer: (B) x + x²/2! + x³/3! + x⁴/4! +….

Explanation

Initially term = 1, sum = 0. Iteration 1 (i = 1): term = 1 * x/1 = x; sum = x Iteration 2 (i = 2): term = x * x/2 = x²/2; sum = x + x²/2 Iteration 3 (i = 3): term = (x²/2) * x/3 = x³/6; sum = x + x²/2 + x³/6 Iteration 4 (i = 4): term = (x³/6) * x/4 = x⁴/24; sum = x + x²/2 + x³/6 + x⁴/24 This is x/1! + x²/2! + x³/3! + x⁴/4! + ....

Q9UGC NET Computer Science, June 2016 (Paper II & III, Selected Questions)
Given i = 0, j = 1, k = –1, x = 0.5, y = 0.0 What is the output of the following expression in C language? x * y < i + j || k
  1. A-1
  2. B0
  3. C1
  4. D2
Show answerHide answer

Correct answer: (C) 1

Explanation

Given i = 0, j = 1, k = –1, x = 0.5, y = 0.0. x * y < i + j || k = (((x * y) < (i + j)) || k) = (((0.5 * 0.0) < (0 + 1)) || –1) = ((0 < 1) || –1) = 1

Q10UGC NET Computer Science, August 2016 (Paper II & III, Selected Questions)
In C++, polymorphism requires :
  1. AInheritance only
  2. BVirtual functions only
  3. CReferences only
  4. DInheritance, Virtual functions and references
Show answerHide answer

Correct answer: (D) Inheritance, Virtual functions and references

Explanation

Basically polymorphism means having many forms. In C++, polymorphism requires Inheritance, Virtual functions and references, because polymorphism occurs when there is a hierarchy of classes and they are related by inheritance which include virtual functions and references.

Q11UGC NET Computer Science, August 2016 (Paper II & III, Selected Questions)
A function template in C++ provides ______ level of generalization.
  1. A4
  2. B3
  3. C2
  4. D1
Show answerHide answer

Correct answer: (C) 2

Explanation

C++ provides two kinds of templates: class templates and function templates. A function template in C++ provides two levels of generalization.

Q12UGC NET Computer Science, June 2017 (Paper II & III, Selected Questions)
Which of the following storage classes have global visibility in C/C++?
  1. AAuto
  2. BExtern
  3. CStatic
  4. DRegister
Show answerHide answer

Correct answer: (B) Extern

Explanation

extern declares variables/functions with external linkage, giving them global visibility across files. It is the default storage class of all global variables and functions.

Q13UGC NET Computer Science, June 2017 (Paper II & III, Selected Questions)
Which of the following operators cannot be overloaded in C/C++?
  1. ABitwise right shift assignment
  2. BAddress of
  3. CIndirection
  4. DStructure reference
Show answerHide answer

Correct answer: (D) Structure reference

Explanation

Structure reference refers to the dot operator (.), which is one of the operators that cannot be overloaded in C++. The other options (>>=, unary & and unary *) can be overloaded for custom types in C++.

Q14UGC NET Computer Science, June 2017 (Paper II & III, Selected Questions)
Which one of the following is correct for overloaded functions in C++?
  1. ACompiler sets up a separate function for every definition of function
  2. BCompiler does not set up a separate function for every definition of function
  3. COverloaded functions cannot handle different types of objects
  4. DOverloaded functions cannot have same number of arguments
Show answerHide answer

Correct answer: (A) Compiler sets up a separate function for every definition of function

Explanation

When you call an overloaded function, the compiler determines the most appropriate definition to use by comparing the arguments types you used to call the function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function is called overload resolution.

Q15UGC NET Computer Science, June 2017 (Paper II & III, Selected Questions)
Given the following statements : (A) A class of language that is closed under union and complementation has to be closed under intersection (B) A class of language that is closed under union and intersection has to be closed under complementation Which of the following options is correct?
  1. ABoth (A) and (B) are false
  2. BBoth (A) and (B) are true
  3. C(A) is true, (B) is false
  4. D(A) is false, (B) is true
Show answerHide answer

Correct answer: (C) (A) is true, (B) is false

Explanation

(A) is true by De Morgan's law: A ∩ B = (A' ∪ B')'. (B) is false: recursively enumerable languages are closed under union and intersection but not under complementation.

Q16UGC NET Computer Science, November 2017 (Paper II & III, Selected Questions)
Which of the following is not correct for virtual function in C++?
  1. AMust be declared in public section of class
  2. BVirtual function can be static
  3. CVirtual function should be accessed using pointers
  4. DVirtual function is defined in base class
Show answerHide answer

Correct answer: (B) Virtual function can be static

Explanation

A virtual function cannot be static, because a virtual function is a non-static member function whose call is resolved through a specific object at run time.

Q17UGC NET Computer Science, November 2017 (Paper II & III, Selected Questions)
Which of the following is not correct (in C++)?
  1. AClass templates and function templates are instantiated in the same way
  2. BClass templates differ from function templates in the way they are initiated
  3. CClass template is initiated by defining an object using the template argument
  4. DClass templates are generally used for storage classes
Show answerHide answer

Correct answer: (A) Class templates and function templates are instantiated in the same way

Explanation

Function templates are instantiated implicitly when the function is called with arguments, whereas class templates are instantiated by defining an object with explicit template arguments. So class templates and function templates are not instantiated in the same way; statement (a) is not correct.

Q18UGC NET Computer Science, November 2017 (Paper II & III, Selected Questions)
A member function can always access the data in ______ , (in C++)
  1. Athe class of which it is member
  2. Bthe object of which it is a member
  3. Cthe public part of its class
  4. Dthe private part of its class
Show answerHide answer

Correct answer: (A) the class of which it is member

Explanation

A member function can always access all the data (public, private and protected) of the class of which it is a member.

Q19UGC NET Computer Science, November 2017 (Paper II & III, Selected Questions)
Context free grammar is not closed under :
  1. AConcatenation
  2. BComplementation
  3. CKleene Star
  4. DUnion
Show answerHide answer

Correct answer: (B) Complementation

Explanation

Context-free languages are closed under union, concatenation and Kleene star, but not under complementation (or intersection).

How to use these UGC NET Computer Science questions

Every question here was asked in a real UGC NET paper; the paper and shift are shown on the card, and the number opens the question on its own page. Try each one before tapping Show answer, then attempt the full UGC NET papers as timed tests to check your speed under exam conditions.