Aptitude Test (C & Java)

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Santanub
S
Santanub
Community Contributor
Quizzes Created: 1 | Total Attempts: 78
| Attempts: 78
SettingsSettings
Please wait...
  • 1/79 Questions

    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");    }}

Please wait...
About This Quiz

This Aptitude Test (C & Java) assesses knowledge in Java programming, focusing on concepts like method overloading, interface inheritance, and static methods. It tests understanding through code execution and theoretical questions, essential for students and professionals in software development.

Aptitude Test (C & Java) - Quiz

Quiz Preview

  • 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 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:

  • 4. 

    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:

  • 5. 

    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:

  • 6. 

    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

    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:

  • 7. 

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

    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:

  • 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

    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:

  • 9. 

    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));    }}

    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:

  • 10. 

    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));    }}

    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:

  • 11. 

    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));    }}

    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:

  • 12. 

    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;    }}

    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:

  • 13. 

    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:

  • 14. 

    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 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:

  • 15. 

    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);        }    }}

    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:

  • 16. 

    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 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:

  • 17. 

    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");    }}

    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:

  • 18. 

    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, "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:

  • 19. 

    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 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:

  • 20. 

    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 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:

  • 21. 

    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"));    }}

    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:

  • 22. 

    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, 2-9};        System.out.println(new Cal().add(i));    }}

    Explanation
    The output of the code is 5 because the add() method takes in a variable number of integers as arguments and adds them together. In the main() method, an array of integers is created with the values {1, 3-2, 5, 7, 2-9}. The expression 3-2 and 2-9 are evaluated to 1 and -7 respectively. Hence, the array becomes {1, 1, 5, 7, -7}. When this array is passed to the add() method, it iterates over each element and adds them together. The sum of the elements is 5, which is then returned and printed as the output.

    Rate this question:

  • 23. 

    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));    }}

    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. The class also has a constructor that takes variable arguments of type int and assigns them to the "a" variable. There is also a method called "add" that takes variable arguments of type "Cal" and returns the sum of all the integers in the "a" arrays of the "Cal" objects passed as arguments.

    In the main method, an integer array "ar" is created and initialized with values. Then, a new instance of "Cal" is created using the array "ar" as an argument. Finally, the "add" method is called on a new instance of "Cal" with the previously created instance as an argument. The sum of all the integers in the "a" array of the "Cal" instance is printed, which is 14.

    Rate this question:

  • 24. 

    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, 3, 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));    }}

    Explanation
    The output of the code is 2020.


    - The `Cal` class has 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 `Cal` object.
    - For each `Cal` object, it iterates over the `a` array and adds each element to the static variable `s`.
    - In the `main` method, two `Cal` objects are created, one with the array {1, 9, 3, 7} and the other with an empty array.
    - The `add` method is called on both `Cal` objects and the result is printed.
    - Since the static variable `s` is not reset between the two method calls, the sum of the elements in the first `Cal` object's `a` array (20) is added to the sum of the elements in the second `Cal` object's `a` array (0), resulting in an output of 2020.

    Rate this question:

  • 25. 

    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 = - 3;    }}

    Explanation
    The code defines a class Cal with an instance variable a and a static variable s. The constructor of Cal takes in a variable number of arguments and assigns them to the instance variable a. The add method takes in a variable number of Cal objects and sums up all the integers in their respective a arrays. In the main method, two instances of Cal are created, one with an array [1, 9, 1, 3] and another with an empty array. The add method is called on both instances and the result is printed. Since the add method sums up all the integers in the array, the sum of [1, 9, 1, 3] is 14 and the sum of an empty array is 0. Therefore, the output is 1111.

    Rate this question:

  • 26. 

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

    Explanation
    The given code is a class named 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 "D".

    The third static block prints "E".

    The fourth static block prints "Z".

    Therefore, when the code is executed, it will print "ADEZ" as the output.

    Rate this question:

  • 27. 

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

    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 successful conversions, in this case, it will return 1. Then, the printf function is used to print the value of 'a', which is 1.

    Rate this question:

  • 28. 

    Int j,k,c=0;for(j=1;j<4;j++)for(k=0;k<4;k++)  c++;printf("%d",c); output?

    Explanation
    The given code snippet initializes three variables: j, k, and c. It then enters a nested loop, with the outer loop iterating from 1 to 3, and the inner loop iterating from 0 to 3. In each iteration of the inner loop, the value of c is incremented by 1. Since there are 3 iterations of the outer loop and 4 iterations of the inner loop, the value of c is incremented a total of 3 * 4 = 12 times. Finally, the value of c is printed, which is 12.

    Rate this question:

  • 29. 

    Int a=500,b=300;if(!(a>b))    printf("True");else    printf("False");output?

    • True

    • TrueFalse

    • False

    • None of these

    Correct Answer
    A. False
    Explanation
    The given code compares the values of variables 'a' and 'b'. The condition inside the 'if' statement is "!(a>b)", which means "not (a is greater than b)". Since a is indeed greater than b (500 is greater than 300), the condition evaluates to false. Therefore, the code will execute the 'else' block and print "False" as the output.

    Rate this question:

  • 30. 

    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");    }

    • Run time error

    • Hello is printed thrice

    • Hello is printed twice

    • Hello is printed infinitely

    Correct Answer
    A. 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:

  • 31. 

    Int a=500,b=300;if(!(a>b))    printf("True");else    printf("False");output?

    • True

    • TrueFalse

    • False

    • None of these

    Correct Answer
    A. False
    Explanation
    The code first checks if the condition !(a>b) is true. In this case, a is not greater than b, so the condition is true. Therefore, the code will execute the statement printf("True"). However, since there is an else statement after the if condition, the code will also execute the statement printf("False"). So, the output will be TrueFalse.

    Rate this question:

  • 32. 

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

    • 3.75

    • Depends on compiler

    • 24

    • 3

    Correct Answer
    A. 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:

  • 33. 

    Int a=4,b=2;if(a%2==0)   printf("hello");   printf("bye");

    • Hello

    • Hellobye

    • Bye

    • None of these

    Correct Answer
    A. Hellobye
    Explanation
    The code snippet first checks if the value of 'a' is divisible by 2 using the modulus operator (%). Since 4 is divisible by 2, the condition is true and the first printf statement "hello" is executed. Then, the second printf statement "bye" is also executed. Therefore, the output will be "hello" followed by "bye", which matches the given answer "hellobye".

    Rate this question:

  • 34. 

    If an exception is generated and there is no matching catch block, default exception handler handles that exception

    • True

    • False

    Correct Answer
    A. True
    Explanation
    If an exception is generated and there is no matching catch block, the default exception handler handles that exception. This means that if an exception is thrown and there is no specific catch block to handle it, the default exception handler will take over and handle the exception. This default handler may display an error message or perform some other default action to handle the exception. Therefore, the statement "True" is correct.

    Rate this question:

  • 35. 

    Int a=3,b=4,c=1;printf("%d%d%d",a+b,scanf("%d%d%d",&a,&b,&c),a+b);inputs are as follow - a=10 b=12 c=13.Hence the ouput is?

    • 22 2 7

    • 22 3 7

    • 35 3 8

    • None of these

    Correct Answer
    A. 22 3 7
    Explanation
    The output is "22 3 7" because the printf function is called with the format string "%d%d%d". The first %d is replaced by the sum of a and b (3+4=7), the second %d is replaced by the return value of the scanf function, which is the number of successful conversions made (3 in this case), and the third %d is replaced by the sum of a and b again (10+12=22). Therefore, the final output is "22 3 7".

    Rate this question:

  • 36. 

    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));    }}

    • 110-1

    • 1

    • 11-1

    • Error

    Correct Answer
    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:

  • 37. 

    Int a=700;if(!a>400)    printf("Jaipur");else     printf("Kolkata");output

    • JaipurKolkata

    • Jaipur

    • Kolkata

    • None of these

    Correct Answer
    A. Kolkata
    Explanation
    The correct answer is "Kolkata" because the condition in the if statement is false. The condition is "!a>400" which means "not a greater than 400". Since a is equal to 700, the condition evaluates to false and the code inside the else block is executed, which prints "Kolkata".

    Rate this question:

  • 38. 

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

    • 10

    • Garbage value

    • Address of i

    • Address of p

    Correct Answer
    A. Address of i
    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:

  • 39. 

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

    • 12

    • 11

    • 10

    • Error

    Correct Answer
    A. 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:

  • 40. 

    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);    }}

    • Error : main does not work with varargs

    • Error : main cannot be called from static block

    • Error : array of String cannot be declared in static block

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

    • Call of duty MW3

    Correct Answer
    A. 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:

  • 41. 

    Int a=1;printf("%d%d%d",a,a++,++a);output?

    • 3 3 1

    • 1 3 3

    • 3 3 2

    • 3 2 2

    Correct Answer
    A. 3 2 2
    Explanation
    The output is 3 2 2. This is because the printf function is called with the format string "%d%d%d". The first %d is replaced with the value of a, which is 1. Then, the second %d is replaced with the value of a++, which is 1, but the value of a is incremented to 2 after the expression is evaluated. Finally, the third %d is replaced with the value of ++a, which is 2 since the value of a is incremented before the expression is evaluated. Therefore, the output is 3 2 2.

    Rate this question:

  • 42. 

    Public class Test {    int dis(int b){        try{            return 10/b;        }catch(Exception ex){            return 1;        }finally{            return 2;        }    }    public static void main(String[] args) {        System.out.println(new Test().dis(0));    }}

    • 1

    • 2

    • 0

    • Error

    Correct Answer
    A. 2
    Explanation
    The code will throw an exception when trying to divide 10 by 0 in the try block. Since there is a catch block that catches any exception, the catch block will execute and return 1. However, the finally block will always execute regardless of whether an exception is thrown or caught. In this case, the finally block will execute and return 2. Therefore, the final output will be 2.

    Rate this question:

  • 43. 

    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);}

    • 55

    • Error

    • Garbage value

    • 56

    Correct Answer
    A. 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:

  • 44. 

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

    • 12

    • 11

    • 10

    • Error

    Correct Answer
    A. 11
    Explanation
    The given code initializes the variable 'c' to 0 and then enters a loop that runs 11 times (from i=1 to i

    Rate this question:

  • 45. 

    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");    }

    • No

    • Hello

    • Hi

    • None of the above

    Correct Answer
    A. 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:

  • 46. 

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

    • 10

    • Garbage value

    • Segmentation error

    • Compile error

    Correct Answer
    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:

  • 47. 

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

    • 10

    • Garbage value

    • Address of i

    • Address of p

    Correct Answer
    A. Address of i
    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:

  • 48. 

    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 

    • 1 2 3 only

    • 1 2 3 4

    • 2 3 4 only

    • 2 3 only

    Correct Answer
    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:

  • 49. 

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

    • Print if the number is odd or even

    • Print if the number is prime or not

    • Print all the factors of a number

    • Will print nothing

    Correct Answer
    A. 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:

Quiz Review Timeline (Updated): Jun 19, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jun 19, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 15, 2016
    Quiz Created by
    Santanub
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.