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.
Q1UGC NET Computer Science, June 2014 (Paper II & III, Selected Questions)
while (87) printf ("computer");
The above C statement will
Aprint "computer" 87 times
Bprint "computer" 0 times
Cprint "computer" 1 times
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
AEscape sequence characters. Format specifies and White-space characters
BSpecial characters, White-space characters and Non-white space characters
CFormat specifies, White-space characters and Non-white space characters
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++);
}
A128
B129
C130
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*/ }
AABCDEFGH1000
BCDEFGH1000
CDDEFGHH1000
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++).
AInaccessible
BAccessible
CProtected
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?
AConstructor
BDestructor
CData Members
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++?
AWhen we write overloaded function we must code the function for each usage
BWhen we write function template we code the function only once
CIt is difficult to debug macros
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?
Ax + x²/2 + x³/3 + x⁴/4 +….
Bx + x²/2! + x³/3! + x⁴/4! +….
C1 + x²/2 + x³/3 + x⁴/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
A-1
B0
C1
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 :
AInheritance only
BVirtual functions only
CReferences only
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.
A4
B3
C2
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++?
AAuto
BExtern
CStatic
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++?
ABitwise right shift assignment
BAddress of
CIndirection
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++?
ACompiler sets up a separate function for every definition of function
BCompiler does not set up a separate function for every definition of function
COverloaded functions cannot handle different types of objects
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?
ABoth (A) and (B) are false
BBoth (A) and (B) are true
C(A) is true, (B) is false
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++?
AMust be declared in public section of class
BVirtual function can be static
CVirtual function should be accessed using pointers
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++)?
AClass templates and function templates are instantiated in the same way
BClass templates differ from function templates in the way they are initiated
CClass template is initiated by defining an object using the template argument
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++)
Athe class of which it is member
Bthe object of which it is a member
Cthe public part of its class
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 :
AConcatenation
BComplementation
CKleene Star
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.