# Aptitude Test (C & Java)

Approved & Edited by ProProfs Editorial Team
At ProProfs Quizzes, our dedicated in-house team of experts takes pride in their work. With a sharp eye for detail, they meticulously review each quiz. This ensures that every quiz, taken by over 100 million users, meets our standards of accuracy, clarity, and engagement.
| Written by Santanub
S
Santanub
Community Contributor
Quizzes Created: 1 | Total Attempts: 66
Questions: 79 | Attempts: 66  Settings  • 1.

### Find the output ...class StaticTest {    static {        System.out.print("A");    }    static {        System.out.print("E");    }    public static void main(String args[]) {    }    static {        System.out.print("Z");    }    static {        System.out.print("D");    }}

Explanation
The given code is a class named "StaticTest" with a main method. The class has multiple static blocks, which are executed in the order they appear in the code.

The first static block prints "A", the second static block prints "E", the third static block prints "Z", and the fourth static block prints "D".

Therefore, the output of the code will be "AEZD".

Rate this question:

• 2.

### Write only outputclass Department {     int code;    String name;     Department() {    }     Department(int c, String n) {        code = c;        name = n;    }     Department[] getAll() {        Department cse = new Department(100, "ME");        Department ce = new Department(101, "EE");        Department me = new Department(102, "ECE");        Department ece = new Department(103, "CE");        Department ee = new Department(104, "CSE");         Department[] dept = {cse, ece, ee, ce, me};         return dept;    }} class College {     public static void main(String... args) {        for (Department d : new Department().getAll()) {            System.out.print(d.name.charAt(1));        }    }}

Explanation
The code provided defines a class called Department with two constructors and a method called getAll(). In the getAll() method, five Department objects are created and stored in an array called dept. The main method in the College class then calls the getAll() method and iterates over the array of Department objects. For each Department object, it prints the character at index 1 of the name string. In this case, the names of the Department objects are "ME", "EE", "ECE", "CE", and "CSE". The character at index 1 of each name is "E", "E", "C", "E", and "S" respectively. Therefore, the output will be "EESEC".

Rate this question:

• 3.

### Write only outputclass Department {     int code;    String name;     Department(int c, String n) {        code = c;        name = n;    }     Department[] getAll() {        Department cse = new Department(100, "CSE");        Department ce = new Department(101, "CE");        Department me = new Department(102, "ME");        Department ece = new Department(103, "ECE");        Department ee = new Department(104, "EE");         Department[] dept = {cse, ece, ee, ce, me};         return dept;    }} class College {     public static void main(String... args) {        for (Department d : new Department().getAll()) {            System.out.print(d.name.charAt(1));        }    }}

Explanation
The code provided is incomplete and will result in a compilation error. The error occurs because the class Department does not have a default constructor, and in the College class, a new instance of Department is created without passing any arguments to the constructor. Therefore, the code will not compile and will produce an error.

Rate this question:

• 4.

### Write only output ...class Department {     static void showName() {        System.out.print("C");    }     static {        showName();        System.out.print("S");    }} class College {     static {        System.out.print("E");    }     public static void main(String... args) {    }}

Explanation
The correct answer is "E" because the static block in the College class is executed first before any other code in the class. In the static block, the statement System.out.print("E") is executed, which prints "E" as the output.

Rate this question:

• 5.

### Write only output ...class Department {     static {        System.out.print("2");    }    static void showName() {        System.out.print("#");    }     static {        System.out.print("#");    }} class College {     static {        System.out.print("M");    }     public static void main(String... args) {    }    static {        System.out.print("I");        Department.showName();    }}

Explanation
The output "MI2##" is obtained by following the sequence of statements in the code.
First, the static block in the Department class is executed, printing "2".
Then, the static block in the College class is executed, printing "M".
Next, the static block in the Department class is executed again, printing "#".
Finally, the main method is called in the College class, but it is empty and does not have any statements to execute.
Therefore, the output is "MI2##".

Rate this question:

• 6.

### Write only output ... class Department{    void showDept(){        System.out.print("All");    }}class CSE extends Department{    void showDept(){        System.out.print("CSE");    }}class EEE extends Department{    void showDept(){        System.out.print("EEE");    }}class EI extends Department{    void showDept(){        System.out.print("EI");    }}class College{    public static void main(String args[]) {        Department d1=new CSE();        Department d2=new EEE();        Department d3=new EI();         d1.showDept();        d2.showDept();        d3.showDept();    }}

Explanation
The given code defines a class hierarchy with a base class Department and three derived classes CSE, EEE, and EI. Each class has a method showDept() that prints a specific department name. In the main method, objects of the derived classes are created and assigned to Department type variables. When the showDept() method is called on these objects, the overridden version of the method in each derived class is executed, printing the respective department name. Therefore, the output of the code is "CSEEEEEI".

Rate this question:

• 7.

### Which are true for variable length argument (modern approach)1. works as an array2. only one parameter can be used as method parameter3. any number of arguments or one array can be passed and parameter/s type must be same as varargs type4. can be used as return type

• A.

1 2 3 only

• B.

1 2 3 4

• C.

2 3 4 only

• D.

2 3 only

A. 1 2 3 only
Explanation
Variable length arguments, also known as varargs, allow a method to accept a variable number of arguments of the same type. In the modern approach, varargs work as an array, allowing multiple values to be passed as arguments. Only one parameter can be used as a method parameter, and it must have the same type as the varargs type. Therefore, the correct answer is "1 2 3 only."

Rate this question:

• 8.

### Class Student {    private Student() {    }    static void show(int... id) {    }    static void show(String... id) {    }    static void show(double... id) {    }    public static void main(String args[]) {        //statement    }} Write only one complete statement to call show method that shows ambiguity problem

Student.show();
Explanation
The statement "Student.show();" causes an ambiguity problem because there are multiple overloaded methods with the same name "show" that can be called. The methods have different parameter types (int, String, double), but since no arguments are provided in the statement, the compiler cannot determine which method to invoke.

Rate this question:

• 9.

### Int a;a=scanf("%d",&a);​printf("%d",a);(user gives 5 as input) what will the output?

1
Explanation
The output will be 1. The code first reads an integer input from the user using the scanf function and stores it in the variable 'a'. The scanf function returns the number of successfully scanned items, which in this case is 1. Then, the printf function is used to print the value of 'a', which is 1.

Rate this question:

• 10.

### Int i,c=0;for(i=1;i<12;i++)c++;printf("%​d",c); output?

• A.

12

• B.

11

• C.

10

• D.

Error

B. 11
Explanation
The given code initializes a variable 'c' to 0 and then enters a for loop that iterates from 1 to 11. In each iteration, the value of 'c' is incremented by 1. Therefore, the loop executes 11 times and at the end, the value of 'c' becomes 11. Finally, the printf statement prints the value of 'c' which is 11.

Rate this question:

• 11.

### Int i,n;scanf("%d",&n);for(i=1;i<=n;i++)   if(!(n%i))        printf("%d\n",i);The following program will_______

• A.

Print if the number is odd or even

• B.

Print if the number is prime or not

• C.

Print all the factors of a number

• D.

Will print nothing

C. Print all the factors of a number
Explanation
The given program takes an input number 'n' and then iterates through all numbers from 1 to 'n'. For each iteration, it checks if 'n' is divisible by the current number 'i' using the condition "!(n%i)". If 'n' is divisible by 'i', then 'i' is a factor of 'n'. Therefore, the program will print all the factors of the input number 'n'.

Rate this question:

• 12.

### Int i,k,n;scanf("%d",&n);for(i=n,k=1;1<=i;i++)        k*=i;printf("%d",k);what will the following program will do?

• A.

It will print the factorial of a number given as input by the user

• B.

It will print the factors of a number given as input by the user

• C.

It will show error

• D.

It will enter infinite loop

A. It will print the factorial of a number given as input by the user
Explanation
The given program uses a for loop to calculate the factorial of a number. It takes an integer input from the user and stores it in the variable 'n'. Then, it initializes 'i' with 'n' and 'k' with 1. The for loop runs as long as 'i' is greater than or equal to 1, and in each iteration, it multiplies 'k' with 'i' and decrements 'i' by 1. Finally, it prints the value of 'k', which is the factorial of the input number. Therefore, the program will print the factorial of a number given as input by the user.

Rate this question:

• 13.

### What will be the output?#include<stdio.h>void main(){    int i;    for(i=0;i<=5;i++);    printf("%d",i);   }

• A.

012345

• B.

6

• C.

Compile error

• D.

5

B. 6
Explanation
The code snippet provided is a simple program that uses a for loop to iterate from 0 to 5. However, there is a semicolon (;) immediately after the for loop declaration, which creates an empty statement. This means that the loop does not have any code block associated with it, and the printf statement outside the loop will only be executed once after the loop iteration is completed. Therefore, the output will be "6".

Rate this question:

• 14.

### What will be output of following c code?        #include<stdio.h>void main(){    int x=123;    int i={         printf("c" "+")    };    for(x=0;x<=i;x++){         printf("%d ",x);    }}

• A.

Compile error

• B.

C++01234

• C.

C +0 1 2

• D.

123123123123

C. C +0 1 2
Explanation
The code will result in a compile error. The line "int i={ printf("c"+" ) };" is incorrect syntax. The concatenation of two string literals is not allowed in C.

Rate this question:

• 15.

### What is the output of this C code?     #include <stdio.h>    void main()    {        int k = 0;        for (k)            printf("Hello");    }

• A.

hello

• B.

Nothing

• C.

Varies

• D.

Compile time error

D. Compile time error
Explanation
The given code will result in a compile time error. This is because the condition in the for loop is missing, which is required for the loop to execute. In C, the condition inside the parentheses of the for loop should evaluate to either true or false. Since there is no condition specified in this code, the compiler will throw an error.

Rate this question:

• 16.

### What is the output of this C code?     #include <stdio.h>    void main()    {        double k = 0;        for (k = 0.0; k < 3.0; k++)            printf("Hello");    }

• A.

Run time error

• B.

Hello is printed thrice

• C.

Hello is printed twice

• D.

Hello is printed infinitely

B. Hello is printed thrice
Explanation
The code will print "Hello" three times. The for loop initializes the variable k to 0.0 and continues to execute as long as k is less than 3.0. In each iteration of the loop, "Hello" is printed. Since the loop increments k by 1 each time, the loop will execute three times (when k is 0.0, 1.0, and 2.0), resulting in "Hello" being printed thrice.

Rate this question:

• 17.

### The output of the code below is     #include <stdio.h>    void main()    {        int x = 5;        if (x < 1)            printf("hello");        if (x == 5)            printf("hi");        else            printf("no");    }

• A.

No

• B.

Hello

• C.

Hi

• D.

None of the above

C. Hi
Explanation
The output of the code is "hi" because the value of x is 5. The first if statement is not true because x is not less than 1, so "hello" is not printed. The second if statement is true because x is equal to 5, so "hi" is printed. The else statement is not executed because the second if statement is true. Therefore, the output is "hi".

Rate this question:

• 18.

### What is the output of this C code?    #include  <stdio.h>    int main()    {       signed char chr;       chr = 128;       printf("%d\n", chr);       return 0;    }

• A.

128

• B.

-128

• C.

Depends on the compiler

• D.

None of the mentioned

B. -128
Explanation
The output of this C code is -128. In this code, a signed char variable named "chr" is assigned the value 128. However, the range of a signed char is -128 to 127. When a value greater than 127 is assigned to a signed char, it overflows and wraps around to the minimum value in its range, which is -128. Therefore, when the printf function is called to print the value of "chr", it will output -128.

Rate this question:

• 19.

### What is the value of x in this C code?    #include     void main()    {        int x = 5 * 9 / 3 + 9;    }

• A.

3.75

• B.

Depends on compiler

• C.

24

• D.

3

C. 24
Explanation
The value of x in this C code is 24. This is because the expression "5 * 9 / 3 + 9" is evaluated according to the order of operations (PEMDAS/BODMAS). First, the multiplication 5 * 9 is performed, resulting in 45. Then, the division 45 / 3 is performed, resulting in 15. Finally, the addition 15 + 9 is performed, resulting in 24. Therefore, the value of x is 24.

Rate this question:

• 20.

### What is the output of this C code?     #include <stdio.h>    void main()    {        int y = 3;        float x = 5 % 2 * y / 2;        printf("Value of x is %f", x);    }

• A.

Value of x is 1.000

• B.

Value of x is 2.000

• C.

Value of x is 1

• D.

Compile time error

A. Value of x is 1.000
Explanation
The output of this C code is "Value of x is 1.000".

In the code, the variable y is assigned a value of 3. Then, the expression "5 % 2 * y / 2" is evaluated. The modulus operator (%) returns the remainder of the division of 5 by 2, which is 1. Then, the multiplication and division operations are performed, resulting in 1.000. Finally, the printf statement prints the value of x as "Value of x is 1.000".

Rate this question:

• 21.

### Int main(){int i = 10,*p = &i;printf("%d\n", *p);}

• A.

10

• B.

Garbage value

• C.

Segmentation error

• D.

Compile error

A. 10
Explanation
The code declares an integer variable i and a pointer variable p, which is assigned the address of i. The printf statement then dereferences the pointer p and prints the value stored at that memory location, which is 10. Therefore, the correct answer is 10.

Rate this question:

• 22.

### Int main(){int i = 10,*p;p= &i;printf("%u\n", p);}

• A.

10

• B.

Garbage value

• C.

• D.

Explanation
The correct answer is "address of i". In the given code, the variable "i" is assigned a value of 10. The pointer variable "p" is then assigned the address of "i" using the "&" operator. The printf statement prints the value of "p", which is the address of "i". Therefore, the output will be the address of "i".

Rate this question:

• 23.

### Int main(){int i = 10,*p;p= &i;printf("%d\n", *(&p));}

• A.

10

• B.

Garbage value

• C.

• D.

Explanation
The given code declares an integer variable i and a pointer variable p. The pointer p is assigned the address of i using the address-of operator (&). The printf statement is used to print the value pointed to by the pointer p, which is the value stored in the variable i. Therefore, the correct answer is "address of i", as it will print the memory address where the variable i is stored.

Rate this question:

• 24.

### Int add(int x,int y){int z=(x+ ++y);return z ;}void main(){ int i=50,b;b= add(i,5) ;printf("%d",b);}

• A.

55

• B.

Error

• C.

Garbage value

• D.

56

D. 56
Explanation
The given code defines a function `add` that takes two integers `x` and `y`, increments `y` by 1 and adds `x` and the incremented value of `y` to get `z`. The function then returns the value of `z`. In the `main` function, an integer `i` is assigned the value 50, and another integer `b` is declared. The `add` function is called with `i` and 5 as arguments, and the returned value is assigned to `b`. Finally, the value of `b` is printed, which is 56.

Rate this question:

• 25.

• A.

55

• B.

Error

• C.

Garbage value

• D.

56

B. Error
Explanation
The code provided is attempting to call the function "add" and assign the result to the variable "b". However, there are a few issues with the code. Firstly, the function "add" is declared after it is called, which will cause a compilation error. Additionally, the function "add" is declared to return void, but it is actually returning an integer value. This will also cause a compilation error. Therefore, the correct answer is "error".

Rate this question:

• 26.

### Int func(int);main(){int i=0;printf("%d",func(i));printf("%d",func(i));}int func(){return (i+1);}

• A.

1,1

• B.

0,1

• C.

1,2

• D.

Garbage value

A. 1,1
Explanation
The code provided defines a function called "func" that takes an integer argument and returns that argument plus one. In the main function, an integer "i" is initialized to 0. Then, the "func" function is called twice, passing "i" as the argument each time. Finally, the returned value from each function call is printed. Since the value of "i" is 0 in both function calls, the return value will always be 1. Therefore, the output will be 1,1.

Rate this question:

• 27.

### Class Student {    private Student() {    }    static void show(int... id) {    }    static void show(String... id) {    }    static void show(double... id) {    }    public static void main(String args[]) {        //statement    }} Write only one complete statement to call show method that shows ambiguity problem

Student.show();,show();
Explanation
The given code defines three static methods with variable arguments of different types (int, String, double). When calling the show method without any arguments, it will result in an ambiguity problem because the compiler cannot determine which method should be invoked. Therefore, the statement "Student.show();, show();" will cause an ambiguity error.

Rate this question:

• 28.

### Which are true1. members in interface are protected implicitly if interface is protected2. members in interface are public implicitly if interface is public3. interface can inherit another interface4. class can inherit any number of interfaces5. if an abstract class implements an interface, we have to override abstract method of the interface

• A.

1 2 3 4 5

• B.

1 2 3 5 only

• C.

1 3 4 5 only

• D.

2 3 4 5 only

• E.

2 3 4 only

E. 2 3 4 only
Explanation
The given correct answer is 2, 3, 4 only. This means that the statements 2, 3, and 4 are true while the statements 1 and 5 are false. Statement 2 states that members in an interface are public implicitly if the interface is public, which is true. Statement 3 states that an interface can inherit another interface, which is also true. Statement 4 states that a class can inherit any number of interfaces, which is true as well. However, statement 1, which states that members in an interface are protected implicitly if the interface is protected, is false. Statement 5, which states that if an abstract class implements an interface, we have to override the abstract method of the interface, is also false.

Rate this question:

• 29.

### Write the output ...public class Cal {     int s;     int add(int... d) {        for (int i : d) {            s += i;        }        return s;    }     public static void main(String args[]) {        int[] i = {1, 3-2, 5, 7, 1-9};        System.out.println(new Cal().add(i));    }}

6
Explanation
The given code defines a class called "Cal" with an instance variable "s" and a method called "add" that takes in a variable number of integers as arguments. The method iterates over the given integers and adds them to the instance variable "s". Finally, the method returns the value of "s".

In the main method, an array of integers "i" is created with the values {1, 3-2, 5, 7, 1-9}. The add method is called on a new instance of the "Cal" class, passing in the array "i" as an argument. The add method adds all the integers in the array to the instance variable "s" and returns the value of "s", which is 6.

Therefore, the output of the code is 6.

Rate this question:

• 30.

### Write the output ... public class Cal {     int[] a;    int s;     Cal(int... v) {        a = v;    }     int add(Cal... d) {        for (Cal c : d) {            for (int i : c.a) {                s += i;            }        }        return s;    }     public static void main(String args[]) {        int[] ar = {9, 7, 5, 3};        Cal cal = new Cal(ar);        System.out.println(new Cal().add(cal));    }}

24
Explanation
The code defines a class called "Cal" with an instance variable "a" of type int array and another instance variable "s" of type int. It also defines a constructor that takes in a variable number of integers and assigns them to the "a" array.

The "add" method takes in a variable number of "Cal" objects and iterates over each object. For each object, it iterates over the "a" array and adds each element to the "s" variable. Finally, it returns the value of "s".

In the main method, an integer array "ar" is created with values [9, 7, 5, 3]. A new "Cal" object "cal" is created with "ar" as the argument for the constructor. The "add" method is called on a new instance of "Cal" without any arguments, passing in "cal" as the argument. The result, which is 24, is printed to the console.

Rate this question:

• 31.

### Write the output ... public class Cal {     int[] a;    static int s;     Cal(int... v) {        a = v;    }     int add(Cal... d) {        for (Cal c : d) {            for (int i : c.a) {                s += i;            }        }        return s;    }     public static void main(String args[]) {        int[] ar = {1, 9, 4, 7};        int[] ar2={};                Cal cal = new Cal(ar);        Cal cal2 = new Cal(ar2);                System.out.print(new Cal().add(cal));        System.out.print(new Cal().add(cal2));    }}

2121
Explanation
The output of the code is 2121. This is because the code creates two instances of the Cal class, cal and cal2, with different arrays as arguments. The add() method in the Cal class takes a variable number of Cal objects as arguments. Inside the add() method, it iterates over each Cal object and then iterates over each element in the array of that object. It adds each element to the static variable s. Since s is a static variable, its value is shared among all instances of the Cal class. Therefore, when add() is called on cal and cal2, the elements of both arrays are added to s. Finally, the value of s is returned, which is 2121.

Rate this question:

• 32.

### Write the output ... public class Cal {     int[] a;    static int s;     Cal(int... v) {        a = v;    }     int add(Cal... d) {        for (Cal c : d) {            for (int i : c.a) {                s += i;            }        }        return s;    }     public static void main(String args[]) {        int[] ar = {1, 9, 1, 3};        int[] ar2={};                Cal cal = new Cal(ar);        Cal cal2 = new Cal(ar2);                System.out.print(new Cal().add(cal));        System.out.print(new Cal().add(cal2));    }     static {         s = - 2;    }}

1212
Explanation
The given code defines a class Cal with an instance variable a (an integer array) and a static variable s (an integer). The class also has a constructor that takes in variable arguments and assigns them to the instance variable a. There is also a method add that takes in variable arguments of type Cal and adds up all the integers in the arrays of those Cal objects and stores the sum in the static variable s. The main method creates two instances of Cal, one with an array [1, 9, 1, 3] and the other with an empty array. It then calls the add method on both instances and prints the value of s. Since s is a static variable, the value is shared between all instances of Cal. The initial value of s is -2, and when add is called on the first instance, it adds up all the integers in the array [1, 9, 1, 3] and stores the sum in s. When add is called on the second instance with an empty array, it does not add anything to s. Therefore, the final value of s is the sum of the integers in the first array, which is 12. Hence, the output is 1212.

Rate this question:

• 33.

### Class StaticTest {    static {        System.out.print("A");    }    static {        System.out.print("D");    }    public static void main(String args[]) {    }    static {        System.out.print("Z");    }    static {        System.out.print("E");    }}

Explanation
The code provided is a class named StaticTest. Inside the class, there are four static blocks, which are executed in the order they appear in the code. Each static block is responsible for printing a specific letter: "A", "D", "Z", and "E". Therefore, when the main method is called, the output will be "ADZE", which matches the given answer.

Rate this question:

• 34.

### Find the output ...class StaticTest {    static {        System.out.print("A");    }    static {        System.out.print("E");    }    public static void main(String args[]) {    }    static {        System.out.print("Z");    }    static {        System.out.print("D");    }}

AEZD
Explanation
The given code defines a class called StaticTest. It contains four static blocks, which are executed in the order they appear in the code.

The first static block prints "A", the second static block prints "E", the third static block prints "Z", and the fourth static block prints "D".

In the main method, there is no code to execute, so it does nothing.

Therefore, the output of the code will be "AEZD".

Rate this question:

• 35.

### Find the output ...class Game {    public static void main(String... args) {        for (String s : args) {            System.out.print(s);        }     }     static {        String[] args = {"Call of duty", " MW3"};        main(args);    }}

• A.

Error : main does not work with varargs

• B.

Error : main cannot be called from static block

• C.

Error : array of String cannot be declared in static block

• D.

Error : array of String cannot be declared using String[]

• E.

Call of duty MW3

E. Call of duty MW3
Explanation
The correct answer is "Call of duty MW3". This is because the main method is called from the static block, passing an array of strings as an argument. The for loop in the main method then iterates over each string in the array and prints it. Therefore, the output will be "Call of duty MW3".

Rate this question:

• 36.

### Class Game {     String title;    int ver;     public Game() {    }     public Game(String title, int ver) {        this.title = title;        this.ver = ver;    }     Game[] getGame() {        Game fifa = new Game("-FIFA", 1);        Game nfs = new Game("-NFS", 2);        Game igi = new Game("IGI", 1);        Game igi2 = new Game("-IGI2", 2);         Game[] game = {igi, igi2, nfs, fifa};         return game;    }} class Computer {     public static void main(String args[]) {        for (Game game : new Game().getGame()) {            System.out.print(game.title + ":" + game.ver);        }    }}

IGI:1-IGI2:2-NFS:2-FIFA:1
Explanation
The code provided defines two classes, "Game" and "Computer". The "Game" class has two constructors, one with no parameters and one with a "title" and "ver" parameter. The "Game" class also has a method called "getGame" which creates four instances of the "Game" class and stores them in an array. The "Computer" class has a main method which iterates over the array returned by the "getGame" method and prints the "title" and "ver" values of each "Game" object. The output "IGI:1-IGI2:2-NFS:2-FIFA:1" indicates that the "title" and "ver" values of each "Game" object are being printed in the correct order.

Rate this question:

• 37.

### Class Department{    public Department(int deptCode, String deptName) {        System.out.print("Dept. Code "+deptCode+" "+deptName);    }}class CSE extends Department{    public CSE(){                }}class College {    public static void main(String... args) {        new CSE();    }}Write only missing statement to get output : Dept. Code 209 CSE

super(209,"CSE");
Explanation
The missing statement in the given code is "super(209, "CSE");". This statement is used to call the constructor of the superclass (Department) with the arguments 209 and "CSE". By including this statement in the constructor of the subclass (CSE), the output "Dept. Code 209 CSE" will be printed when the CSE object is created in the main method.

Rate this question:

• 38.

### Write only outputclass Department {     int code;    String name;     Department() {    }     Department(int c, String n) {        code = c;        name = n;    }     Department[] getAll() {        Department cse = new Department(100, "ME");        Department ce = new Department(101, "EE");        Department me = new Department(102, "ECE");        Department ece = new Department(103, "CE");        Department ee = new Department(104, "CSE");         Department[] dept = {cse, ece, ee, ce, me};         return dept;    }} class College {     public static void main(String... args) {        for (Department d : new Department().getAll()) {            System.out.print(d.name.charAt(1));        }    }}

EESEC
Explanation
The code provided defines a class Department with two constructors and a method getAll(). The getAll() method creates five Department objects with different codes and names and stores them in an array. In the main method of the College class, a for loop is used to iterate over the array returned by the getAll() method. For each Department object, the second character of the name is printed. In this case, the output is "EESEC" because the second character of each department name (ME, EE, ECE, CE, CSE) is "E".

Rate this question:

• 39.

### Write only outputclass Department {     int code;    String name;     Department(int c, String n) {        code = c;        name = n;    }     Department[] getAll() {        Department cse = new Department(100, "CSE");        Department ce = new Department(101, "CE");        Department me = new Department(102, "ME");        Department ece = new Department(103, "ECE");        Department ee = new Department(104, "EE");         Department[] dept = {cse, ece, ee, ce, me};         return dept;    }} class College {     public static void main(String... args) {        for (Department d : new Department().getAll()) {            System.out.print(d.name.charAt(1));        }    }}

Error, error, ERROR
Explanation
The given code will result in an error because the getAll() method is being called on a new instance of the Department class, which does not have a constructor with no arguments. Therefore, when the for-each loop tries to iterate over the array returned by getAll(), it will throw an error.

Rate this question:

• 40.

### Class Department {     static void showName() {        System.out.print("1");    }     static {        System.out.print("2");    }} class College {     static {        System.out.print("3");    }     public static void main(String... args) {        Department.showName();    }    static {        System.out.print("4");    }}

3421
Explanation
The code snippet provided consists of two classes, Department and College. The Department class has a static method called showName() which prints "1". The College class has a main method which calls the showName() method from the Department class.

The static blocks in the code are executed when the class is loaded. The static block in the Department class prints "2" and the static block in the College class prints "3".

Therefore, when the main method is executed, the output will be "3" followed by "4" from the static blocks in the College class, and then "2" and "1" from the showName() method in the Department class.

Hence, the correct answer is 3421.

Rate this question:

• 41.

### Write only output ...class Department {     static void showName() {        System.out.print("C");    }     static {        showName();        System.out.print("S");    }} class College {     static {        System.out.print("E");    }     public static void main(String... args) {    }}

E
Explanation
The correct answer is "E" because the static block in the College class is executed first before any other code in the class. In the static block, "E" is printed to the console. Since there is no other code in the main method or any other static block, "E" is the only output.

Rate this question:

• 42.

### Write only output ...class Department {     static {        System.out.print("2");    }    static void showName() {        System.out.print("#");    }     static {        System.out.print("#");    }} class College {     static {        System.out.print("M");    }     public static void main(String... args) {    }    static {        System.out.print("I");        Department.showName();    }}

MI2##
Explanation
The given code consists of two classes, Department and College. The static blocks in the Department class are executed first, printing "2#" on the console. Then, the static blocks in the College class are executed, printing "M" and "I" on the console. Finally, the main method in the College class is called, but it is empty, so no additional output is generated. Therefore, the output of the code is "MI2##".

Rate this question:

• 43.

### Write only output ... class Department{    void showDept(){        System.out.print("All");    }}class CSE extends Department{    void showDept(){        System.out.print("CSE");    }}class EEE extends Department{    void showDept(){        System.out.print("EEE");    }}class EI extends Department{    void showDept(){        System.out.print("EI");    }}class College{    public static void main(String args[]) {        Department d1=new CSE();        Department d2=new EEE();        Department d3=new EI();         d1.showDept();        d2.showDept();        d3.showDept();    }}

CSEEEEEI
Explanation
The code creates objects of different classes that extend the Department class. Each subclass has its own implementation of the showDept() method. The objects are then assigned to variables of the Department type. When the showDept() method is called on each object, the method of the corresponding subclass is executed. Therefore, the output is "CSEEEEEI", as the showDept() method of the CSE class is called first, followed by the showDept() methods of the EEE and EI classes.

Rate this question:

• 44.

### Class Department{    String name="S";    void showDept(Department d){        System.out.print(d.name);    }}class CSE extends Department{    CSE(String n) {        name=n;    }}class EEE extends Department{    EEE(String n) {        name=n;    }}class EI extends Department{    EI(String n) {        name=n;    }}class College{    public static void main(String args[]) {        Department d=new Department();        d.showDept(new CSE("S"));        d.showDept(new EEE("E"));        d.showDept(new EI("I"));    }}

SEI
Explanation
The given code defines a class hierarchy with a base class "Department" and three derived classes "CSE", "EEE", and "EI". Each derived class has a constructor that takes a string parameter and assigns it to the "name" variable inherited from the base class. The "showDept" method in the base class prints the value of the "name" variable. In the main method, three objects of the derived classes are created and passed to the "showDept" method of the base class. The "name" values passed are "S" for CSE, "E" for EEE, and "I" for EI. Therefore, the output of the program is "SEI".

Rate this question:

• 45.

### Find outputclass System2 {    int d;    void display() {        try {            System.out.print("0");            return d/0;        } catch(Exception ex) {            System.out.print("2");                     return -1;                } finally {            System.out.print("f");        }    }    public static void main(String args[]) {        System.out.print(new System2().display());    }}

• A.

Error

• B.

02f-1

• C.

02-1f

• D.

0

A. Error
Explanation
The code in the given question will result in an error. The display() method attempts to return the value of d divided by 0, which is not allowed and will throw an ArithmeticException. However, the catch block will catch this exception and print "2" before returning -1. Finally, the "f" will be printed. So the output will be "02f-1".

Rate this question:

• 46.

### Find output class System2 {    int opp(int a, int b) {        try {            System.out.print("1");            return a/b;        }catch(Exception ex) {            System.out.print("1");                    return -1;    }finally {            System.out.print("0");        }    }    public static void main(String args[]) {        System.out.print(new System2().opp(2,0));    }}

• A.

110-1

• B.

1

• C.

11-1

• D.

Error

A. 110-1
Explanation
The output is "110-1" because the method opp(2,0) is called in the main method. Inside the opp() method, a try-catch-finally block is used. In the try block, the statement "System.out.print("1");" is executed, and then a division operation "return a/b;" is performed. Since the division by zero is not allowed, an exception is thrown. The catch block catches the exception, prints "1", and returns -1. Finally, the finally block is executed and "0" is printed. Therefore, the output is "110-1".

Rate this question:

• 47.

### Find the true statement ...1. NullPointerException is checked exception2. ArithmeticException is checked exception3. ClassNotFoundException is unchecked exception

• A.

All true

• B.

All false

• C.

Only 1 and 2

• D.

Only 3

B. All false
Explanation
The given answer states that all three statements are false. However, this is incorrect. The correct explanation is that statement 3 is true, while statements 1 and 2 are false. NullPointerException and ArithmeticException are both unchecked exceptions, meaning they do not need to be declared in a method's throws clause or caught in a try-catch block. On the other hand, ClassNotFoundException is a checked exception, which means it must be either declared or caught.

Rate this question:

• 48.

### Find output class System2 {    int opp(int a, int b) {    try {            System.out.print(a/b);        } catch(Exception ex) {            System.out.print(a+b);    } catch(ArithmeticException ex) {            System.out.print(a-b);    }catch(NumberFormatException ex) {            System.out.print(a*b);    }    }    public static void main(String args[]) {        System.out.print(new System2().opp(2,0));    }}

• A.

2

• B.

0

• C.

Exception

• D.

Compilation Error

D. Compilation Error
Explanation
The given code will result in a Compilation Error. This is because the catch blocks are not ordered correctly. In Java, catch blocks should be ordered from most specific to least specific. In this case, the catch block for ArithmeticException should come before the catch block for Exception. However, in the given code, the catch block for Exception comes first, which causes a compilation error.

Rate this question:

• 49.

### Find output class System {    int opp(int a, int b) {    try {            System.out.print(a/b);        } catch(Exception ex) {            System.out.print(a-b);    }    }    public static void main(String args[]) {        System.out.print(new System().opp(2,0));    }}

• A.

Compilation Error

• B.

0

• C.

2

• D.

-2

A. Compilation Error
Explanation
The code will result in a compilation error because the method opp() does not have a return statement. The method is declared as returning an int, but there is no return statement in the try or catch block. Therefore, the code will not compile.

Rate this question:

• 50.

### Subclasses catch exception as type of that class and also its Superclasss

• A.

True

• B.

False Back to top