Search Previous Year Questions

Find real questions from previous competitive exam papers with answers and solutions.

Leave the keyword blank to browse saved questions for your selected exam, newest papers first. Add a keyword, year or topic to narrow the results.

Browse complete papers
Showing 10 question(s)
Clear

Question 141

UP police programmer Grade 2 2013• Part 2: Computer Programming

Output of the following code is

int i=5;

while(i++<10)

cout<<i;

  1. AOption (a)

    5 6 7 8 9 

  2. BOption (b)

    5 6 7 8 9 10

  3. COption (c)

    6 7 8 9

  4. DOption (d)

    6 7 8 9 10

View answer & explanation

Correct answer

Answer: (d)D.

6 7 8 9 10

Why this answer is correct

The correct answer is : option 4

Key Points

Code:

int i = 5;

while(i++ < 10) cout << i;

Concept:

The expression i++ is a post-increment, meaning:

  • The condition i++ < 10 evaluates using the current value of i.
  • Then i is incremented after the comparison.
  • Inside the loop, cout << i prints the incremented value.

 

Step-by-Step Execution:

Loop Iterationi (before i++)Condition (i++ < 10)i (after increment)Printed
15true66
26true77
37true88
48true99
59true1010
610false11—

Final Output: 6 7 8 9 10

Correct Answer: Option 4) 6 7 8 9 10 ✅

Question 142

UP police programmer Grade 2 2013• Part 2: Computer Programming

To store the value 100 in each of the elements of the array A declared by

int [ ] A = new int[12];

Which of the following for loops would do this correctly ?

  1. AOption (a)

    for (int k = 1; k <= 12; k++)

    A[k] = 100;

  2. BOption (b)

    for (int k = 0; k <= 12; k++)

    A[k] = 100;

  3. COption (c)

    for (int k = 1; k <= 11; k++)

    A[k] = 100;

  4. DOption (d)

    for (int k = 0; k <= 11; k++)

    A[k] = 100;

View answer & explanation

Correct answer

Answer: (d)D.

for (int k = 0; k <= 11; k++)

A[k] = 100;

Why this answer is correct

The correct answer is : option 4

Key Points Goal:

Store the value 100 in each element of array A.

Option Analysis:

Option 1:

for (int k = 1; k <= 12; k++) A[k] = 100;

❌ Incorrect — Starts from index 1, skips index 0, and tries to access index 12 (out of bounds).

Option 2:

for (int k = 0; k <= 12; k++) A[k] = 100;

❌ Incorrect — Tries to access A[12], which does not exist. Array ends at A[11].

Option 3:

for (int k = 1; k <= 11; k++) A[k] = 100;

❌ Incorrect — Skips index 0, so A[0] remains uninitialized (0).

Option 4:

for (int k = 0; k <= 11; k++) A[k] = 100;

✅ Correct — Covers all valid indices from 0 to 11 (inclusive).

Correct Answer: Option 4) ✅

Question 143

UP police programmer Grade 2 2013• Part 2: Computer Programming

What is the following code segment determining ?

int recipe(int a){

int n = a << 2;

return (n % 2 == 0 ? 1:0);

}

int secret(int a[ ],

int n){ int s = 0, i;

for (i = 0; i < n; i++)

if (recipe(a[i]))

s += a[i];

return s;

}

 main() {

int a[] = {1, 2, 3, 4, 5};

printf(“%d”, secret(a, 5));

}

  1. AOption (a)

    The program is finding the sum of the array elements

  2. BOption (b)

    The program is finding twice of the sum of the array elements

  3. COption (c)

    The program is finding the sum of the even indexed array elements

  4. DOption (d)

    The program is finding the sum of the even numbers in the array

View answer & explanation

Correct answer

Answer: (a)A.

The program is finding the sum of the array elements

Why this answer is correct

The correct answer is : option 1

Key Points

Code Overview:

int recipe(int a){ int n = a << 2; return (n % 2 == 0 ? 1 : 0); }

int secret(int a[], int n){ int s = 0, i; for (i = 0; i < n; i++) if (recipe(a[i])) s += a[i]; return s; }

int main(){ int a[] = {1, 2, 3, 4, 5}; printf("%d", secret(a, 5)); }

Step-by-Step Explanation:

Function recipe(int a):

  • a << 2 is a left shift by 2 bits, i.e., multiplies a by 4.
  • n % 2 == 0 checks if the result is even.
  • Observation: Any number multiplied by 4 is always even → so n % 2 == 0 is always true.
  • However, let’s validate this logic with actual values to be safe.

Test with values:

a = 1 → 1 << 2 = 4 → even → return 1 a = 2 → 8 → even → return 1 a = 3 → 12 → even → return 1 a = 4 → 16 → even → return 1 a = 5 → 20 → even → return 1

So, all values return 1 → all elements are included in the sum.

Function secret(): Sums up all elements for which recipe() returns 1.

Array a[] = {1, 2, 3, 4, 5} → All included

Total sum = 1 + 2 + 3 + 4 + 5 = 15

Final Output: 15

Correct Answer: Option 1) The program is finding the sum of the array elements ✅

Question 144

UP police programmer Grade 2 2013• Part 2: Computer Programming

Output of the following code is

int a=10;

int &b=a;

++b;

cout<<“a=”<<a<<endl<<“b=”<<b;

  1. AOption (a)

    a=10

    b=10

  2. BOption (b)

    a=10

    b=11

  3. COption (c)

    a=11

    b=11

  4. DOption (d)

    a=11

    b=10

View answer & explanation

Correct answer

Answer: (c)C.

a=11

b=11

Why this answer is correct

The correct answer is : option 3

Key Points

Code:

int a = 10; int &b = a; ++b; cout << "a=" << a << endl << "b=" << b;

Concept:

In C++, a reference (&b) is another name (alias) for the same memory location as the variable a. Any change made using b will directly affect a.

Step-by-Step Execution:

  1. a = 10; → Variable a is initialized with value 10.
  2. int &b = a; → b is a reference to a.
  3. ++b; → Increments the value of b, which is same as a. So, a becomes 11.
  4. cout prints the values of a and b.

Final Output:

a=11 b=11

Correct Answer: Option 3) a=11 b=11 ✅

Question 145

UP police programmer Grade 2 2013• Part 2: Computer Programming

In system security, the ___________ threat is a type of attack in which one system assumes the identity of another. 

  1. AOption (a)

    spam 

  2. BOption (b)

    masquerade 

  3. COption (c)

    spoofing 

  4. DOption (d)

    All of the above

View answer & explanation

Correct answer

Answer: (b)B.

masquerade 

Why this answer is correct

The correct answer is: option 2) masquerade

Key Points

  • Masquerade: ✅ A masquerade attack occurs when one entity pretends to be another by falsifying data to gain unauthorized access to resources. This is a common security threat in systems.
  • Spam: ❌ Spam refers to unsolicited bulk messages, typically sent via email, and is not related to identity impersonation.
  • Spoofing: ❌ Spoofing involves forging data to deceive, often in network communication or emails, but it is not specifically about assuming another system's identity.
  • All of the above: ❌ This option is incorrect because not all the listed terms fit the scenario described in the question.

Additional Information

  • Masquerade attacks: These often involve stealing credentials or using methods like phishing to impersonate a legitimate user or system.
  • System Security: Implementing strong authentication mechanisms can help prevent masquerade attacks.

Hence, the correct answer is: option 2) masquerade

Question 146

UP police programmer Grade 2 2013• Part 2: Computer Programming

Which of the following code segment will not result in a ‘null’ terminated character array ? 

  1. AOption (a)

    char a1[ ] = “Try”;

  2. BOption (b)

    char a2[10] = {‘T’,‘r’,‘y’};

  3. COption (c)

    char a3[10]; a

    3[0] = ‘T’;

    a3[1] = ‘r’;

    a3[1] = ‘y’;

  4. DOption (d)

    char *a4 = “Try”;

View answer & explanation

Correct answer

Answer: (a)A.

char a1[ ] = “Try”;

Why this answer is correct

The correct answer is :option 1

 Key Points

In C, a null-terminated character array (i.e., a string) ends with the special character '\0', which denotes the end of the string. This is important for string functions like printf, strlen, etc., to work correctly.

Option Analysis:

Option 1:

char a1[] = "Try";

✅ Null-terminated — This automatically includes '\0' at the end.

Option 2:

char a2[10] = {'T', 'r', 'y'};

❌ Not null-terminated — You manually provided characters without a '\0'. The rest are zeros, but '\0' is not immediately after the last character. Functions like printf("%s", a2) will not work properly.

Option 3:

char a3[10]; a3[0] = 'T'; a3[1] = 'r'; a3[1] = 'y';

❌ Also incorrect — First, a3[1] is assigned twice, so 'r' is overwritten by 'y'. Also, there's no null character added to terminate the string.

Option 4:

char *a4 = "Try";

✅ Null-terminated — String literals in C are always null-terminated. So, a4 points to {'T', 'r', 'y', '\0'}.

Correct Answer: Option 2) char a2[10] = {'T','r','y'}; ❌ (Not null-terminated)

Question 147

UP police programmer Grade 2 2013• Part 2: Computer Programming

Which of the following is TRUE ? 

  1. AOption (a)

    In Java, an instance field declared public generates a compilation error. 

  2. BOption (b)

    Int is the name of a class available in the package java.lang 

  3. COption (c)

    Instance variable names may only contain letters and digits.

  4. DOption (d)

    A class has always a constructor (possibly automatically supplied by the java compiler)

View answer & explanation

Correct answer

Answer: (d)D.

A class has always a constructor (possibly automatically supplied by the java compiler)

Why this answer is correct

The correct answer is : option 4

Key Points

Option 1: In Java, an instance field declared public generates a compilation error.

❌ False — In Java, it is legal to declare an instance field public. Though not recommended due to encapsulation practices, it does not cause a compilation error.

Option 2: Int is the name of a class available in the package java.lang.

❌ False — int is a primitive data type in Java, and not a class. The corresponding wrapper class is Integer, which belongs to java.lang.

Option 3: Instance variable names may only contain letters and digits.

❌ False — Variable names in Java can include letters, digits, underscores (_), and dollar signs ($). However, they must not begin with a digit. So this statement is too restrictive.

Option 4: A class has always a constructor (possibly automatically supplied by the Java compiler)

✅ True — If a class does not define any constructor, the Java compiler automatically provides a default constructor (with no arguments). So, every class has at least one constructor either explicitly or implicitly.

Correct Answer: Option 4) ✅$

Question 148

UP police programmer Grade 2 2013• Part 2: Computer Programming

What is byte code in the context of Java ? 

  1. AOption (a)

    The type of code generated by a Java compiler 

  2. BOption (b)

    The type of code generated by a Java Virtual Machine

  3. COption (c)

    It is another name for a Java source file

  4. DOption (d)

    It is the code written within the instance methods of a class

View answer & explanation

Correct answer

Answer: (a)A.

The type of code generated by a Java compiler 

Why this answer is correct

Below is the detailed solution for the provided question in the specified HTML format: ---

The correct answer is Option 1

Key Points

  • Bytecode: ✅ Bytecode is the intermediate code generated by the Java compiler after compiling a Java source file. It is not directly understood by the machine but needs to be executed by the Java Virtual Machine (JVM).
  • JVM: Java Virtual Machine (JVM) translates the bytecode into machine code (specific to the underlying hardware) during runtime, enabling the platform-independent nature of Java.
  • Source File: ❌ A Java source file is the file written by the programmer (with a .java extension), not the compiled bytecode.
  • Instance Methods: ❌ Bytecode is not tied specifically to the code inside instance methods; it represents the compiled version of the entire Java source file.

Additional Information

  • Portability: Bytecode allows Java to be platform-independent because it runs on the JVM, which is available for various platforms (Windows, Linux, Mac, etc.).
  • Extension: Bytecode is stored in files with the .class extension and is the input for the JVM.

Hence, the correct answer is: Option 1) The type of code generated by a Java compiler

--- This explanation provides a detailed understanding of what bytecode is in the context of Java, why it is important, and why the other options are incorrect.

Question 149

UP police programmer Grade 2 2013• Part 2: Computer Programming

Consider the following Java code

class x{

private int a;

private int b;

public synchronized void addA(){ a

++;

} p

ublic synchronized void addB(){

b++;

}

} 

Can 2 threads access the same instance of class x performing x.addA() and x.addB() at the same time ?

  1. AOption (a)

    Yes 

  2. BOption (b)

    No

  3. COption (c)

    Yes, but sometime it may result in Runtime Exception

  4. DOption (d)

    Compile Error

View answer & explanation

Correct answer

Answer: (b)B.

No

Why this answer is correct

The correct answer is: option 2.

Key Points

Code Summary:

class x { private int a; private int b;

public synchronized void addA() { a++; }

public synchronized void addB() { b++; } }

Explanation:

  • Both addA() and addB() are synchronized instance methods.
  • This means: when one thread enters addA() or addB(), it locks the object-level monitor of the current instance.
  • Only one thread can access any synchronized method of that object at a time.
  • So, if thread T1 is executing addA(), thread T2 cannot enter addB() until T1 releases the lock.

Conclusion:

❌ No — Two threads cannot execute addA() and addB() concurrently on the same object if both methods are synchronized.

Correct Answer: Option 2) No ✅

Question 150

UP police programmer Grade 2 2013• Part 2: Computer Programming

ASCII stands for ____________ 

  1. AOption (a)

    American Standard Code for Information Interchange 

  2. BOption (b)

    American System Code for Information Interchange 

  3. COption (c)

    American Security Code for Information Interchange

  4. DOption (d)

    American Scientific Code for Information Interchange

View answer & explanation

Correct answer

Answer: (a)A.

American Standard Code for Information Interchange 

Why this answer is correct

The correct answer is American Standard Code for Information Interchange

Key Points

  • American Standard Code for Information Interchange (ASCII): ✅ ASCII is a character encoding standard for electronic communication. It represents text in computers, telecommunications equipment, and other devices that use text.
  • American System Code for Information Interchange: ❌ This option is incorrect as it is not the correct expansion of ASCII.
  • American Security Code for Information Interchange: ❌ ASCII does not relate to security codes.
  • American Scientific Code for Information Interchange: ❌ ASCII is not a scientific code; it is used for text representation.

Additional Information

  • History: ASCII was first developed in 1963 and is based on the English alphabet.
  • Encoding: ASCII uses 7 bits to represent each character, allowing for 128 unique characters, including letters, numbers, punctuation marks, and control codes.
  • Use Case: ASCII is widely used in programming, data storage, and communication protocols.

Hence, the correct answer is: option 1) American Standard Code for Information Interchange