UGC NET Programming Language

Programming Language (2021-2025)

Distribute Education like Computer Geek

Q1 – Consider the following code segment
int arr[] = {0, 1, 2, 3, 4};
int i = 1, *ptr;
ptr = arr + 2;

 

Arrange the following printf statements in the increasing order of their output.
(a) printf(“%d”, ptr[i]);
(b) printf(“%d”, ptr[i+1]);
(c) printf(“%d”, ptr[-i]);
(d) printf(“%d”, ptr[-i+1]);

 

Choose the correct answer from the options given below
  1. (c), (a), (b), (d)
  2. (c), (d), (a), (b)
  3. (d), (a), (b), (c)
  4. (a), (b), (d), (c)

(UGC NET DEC 2023)

Ans – (2)

Explanation – (a) print ptr[i], it means *(ptr + i)

Now, ptr = 2 and i = 1, So, arr[3] = 3

(b) print ptr[i+1], it means *(ptr + i + 1)

Now, ptr = 2 and i = 1, So, arr[4] = 4

(c) print ptr[-i], it means *(ptr – i)

Now, ptr = 2 and i = 1, So, arr[1] = 1

(d) print ptr[-i + 1], it means *(ptr – i + 1)

Now, ptr = 2 and i = 1, So, arr[2] = 2

Option 2 is the correct, because (c), (d), (a), (b) is in the increasing order.

Q2 – What is the output of the following program?
# include <stdio.h>
# define SQR(x) (x*x)
         int main()
{  int a, b = 3;
        a = SQR(b + 2);
        printf(“%d”, a);
        return 0;
}
  1. 25
  2. 11
  3. Garbage value
  4. 24

(UGC NET DEC 2023)

Ans – (2)

Explanation – Step by step analyze

Variable b = 3 and a is also a variable.

a = SQR(b + 2) // SQR(x) = (x*x)

So, it means (b + 2*b + 2)

Multiplication has higher precedence than addition, so (b + (2*b) + 2) and b = 3

Then, (3 + (2*3) + 2) = 11

Q3 – Which of the statement are CORRECT?
(a) Constructors are invoked automatically when the objects are created.
(b) Constructors do not have return types, not even void and therefore they cannot return values.
(c) Constructors cannot be inherited though a derived class can call the base class constructors.
(d) Constructors can be declared as virtual

 

Choose the correct answer from the options given below:
  1. (a), (b) and (d) only
  2. (a), (b) and (c) only
  3. (b), (c) and (d) only
  4. (a), (c) and (d) only

(UGC NET DEC 2023)

Ans – (2)

Explanation – (a) It has been established that a constructor, which is a special member function in OOP, initializes an object when an instance of a class or object of a class is created. Hence, this statement is correct.

(b) In programming languages such as C++ and Java, a constructor is a special function that initializes an object when it is created, and it does not return a value. Hence it is not like the normal functions.

(c) This is again a correct statement. Constructors can never be inherited by derived classes, but derived classes can explicitly call their base class constructors using either super() (in Java) or BaseClassName() (in C++).

(d) A constructor can never be declared virtual, as they are used for initializing an object in C++. In fact, the existence of virtual functions depends on an object which is already initialized. So, this statement is wrong.

Q4 – What is the output of the following program?
#include<stdio.h>
int main()
{ int i = 3;
while (i–)
{ int i = 10;
        i–;
   printf(“%d”, i);
}
   printf(“%d”, i);
}
  1. 990
  2. 9990
  3. 999 – 1
  4. 99 – 1

(UGC NET DEC 2023)

Ans – (3)

Explanation – The outer loop, i = 3

1st iteration

The inner while loop has a condition (i–). So, original value of i is 3, so that condition becomes true and later on i is equal to 2.

Inside the while loop, there is a new variable i which is declared to 10, and decremented (i–) to 9 and in the inner loop 9 is printed.

2nd iteration

The outer loop, i is now 2.

The inner while loop has a condition (i–). So, original value of i is 2, so that condition becomes true and later on i is equal to 1.

Inside the while loop, there is a new variable i which is declared to 10, and decremented (i–) to 9 and in the inner loop 9 is printed.

3rd iteration

The outer loop, i is now 1.

The inner while loop has a condition (i–). So, original value of i is 1, so that condition becomes true and later on i is equal to 0.

Inside the while loop, there is a new variable i which is declared to 10, and decremented (i–) to 9 and in the inner loop 9 is printed.

4th iteration

The outer loop, i is now 0.

The inner while loop has a condition (i–). So, original value of i is 0, so that condition becomes false and later on i is equal to -1.

Then, outer loop has print instruction. -1 is printed and therefore, option 3 is correct.

Q5 – Given below are two statements:
Statement I – The friend function and the member functions of a friend class directly access the private and protected data.

 

Statement II – The friend function can access the private data through the member functions of the base class.

 

In the light of the above statements, choose the most appropriate answer from the options given below:
  1. Both Statement I and Statement II are correct.
  2. Both Statement I and Statement II are incorrect.
  3. Statement I is correct but Statement II is incorrect.
  4. Statement I is incorrect but Statement II is correct.

(UGC NET DEC 2023)

Ans – (1)

Explanation – A friend function is one which is allowed to access private and protected members of a particular class directly. In the same way, a friend class is a class to which access is granted to its member functions which in turn can manipulate the private and protected members of another class. So, Statement I is correct.

Though a friend function does not directly become a friend of the base class, it can still invoke public or perhaps protected member functions of that base class.

Being the members of the base class, they are allowed to directly access the base class private data.

So, the friend function gets a backdoor into that private data through a protected member function of the base class. So, Statement II is correct.

Q6 – Which of the following statements is TRUE?
  1. Virtual functions do not implement polymorphism
  2. Virtual functions do not permit calling of derived class functions using a base class pointer
  3. We can never build an object from a class containing a pure virtual function
  4. Pure virtual functions can never have a body

(UGC NET DEC 2023)

Ans – (3)

Explanation – A virtual function is a type of function that is declared in the base class and is meant to be overridden in the derived class. The method is essential for runtime polymorphism to allow the correct function to be called for an object even if it is accessed through a pointer or reference to the base class.

In the first statement, actually the whole part of Calling functions that allow polymorphism depends on what we call virtual functions in C++. So, option 1 is false.

The second statement tells us that with virtual functions, we cannot call derived class functions using a base class pointer. This statement is also false since that was actually the very reason why virtual functions had been provided for us.

The fourth statement says that a pure virtual function can never have a body, which is false. Pure virtual function declaration uses = 0 syntax, while sometimes it may happen that a base class may give a pure virtual function an implementation.

The third statementwhich says we can never build an object from a class containing a pure virtual function, is true. A class is considered an abstract class if it has at least one pure virtual function, and thus we can’t directly instantiate an abstract class. Rather, classes derived from it that provide implementations for all of its pure virtual functions can be instantiated.

BOOKS