Core Java Mock Test-3

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Leonfernando
L
Leonfernando
Community Contributor
Quizzes Created: 1 | Total Attempts: 666
Questions: 45 | Attempts: 666

SettingsSettingsSettings
Core Java Mock Test-3 - Quiz

INTRODUCTION TO OOPS
INTRODUCTION TO JAVA AND SDE
LANGUAGE FUNDAMENTALS AND OPERATORS


Questions and Answers
  • 1. 

    Consider the following code: 1. class Test { 2. public static void main(String args[]) { 3. double d = 12.3; 4. Dec dec = new Dec(); 5. dec.dec(d); 6. System.out.println(d); 7. } 8. } 9. class Dec{ 10. public void dec(double d) { d = d - 2.0d; } 11. } Which of the following gives the correct value printed at line 6?

    • A.

      Prints: 12.3

    • B.

      Prints: -2.0

    • C.

      Prints: 10.3

    • D.

      Prints: 0.0

    Correct Answer
    A. Prints: 12.3
    Explanation
    In this code, a double variable "d" is initialized with the value 12.3. Then, an object of the class "Dec" is created and its method "dec" is called, passing the value of "d" as an argument. Inside the "dec" method, the argument "d" is subtracted by 2.0 and assigned back to "d". However, this does not affect the value of the original "d" variable outside the method. Therefore, when the value of "d" is printed at line 6, it still remains 12.3.

    Rate this question:

  • 2. 

    Consider the following code: public class LabeledBreak2 { public static void main(String args[]) { loop: for(int j=0; j<2; j++) { for(int i=0; i<10; i++) { if(i == 5) break loop; System.out.print(i + " "); } } } } Which of the following will be the output for the above code?

    • A.

      0 1 2 3 4 5

    • B.

      Indefinite Loop

    • C.

      1 2 3 4 5

    • D.

      0 1 2 3 4

    • E.

      0 1 2 3 4 0 1 2 3 4

    Correct Answer
    D. 0 1 2 3 4
    Explanation
    The code contains a nested loop. The outer loop runs twice and the inner loop runs 10 times. Inside the inner loop, there is a condition that checks if the value of i is equal to 5. If it is, then the loop labeled "loop" is broken, causing the program to exit the inner loop and continue with the next iteration of the outer loop. Therefore, the output will be 0 1 2 3 4, as the inner loop is terminated before reaching the value 5.

    Rate this question:

  • 3. 

    Consider the following scenario: Real Chocos Private Limited deals in manufacturing variety of chocolates. This organization manufactures three varieties of chocolates. 1. Fruit Chocolates 2. Rum Chocolates 3. Milk Chocolates A software system needs to be built. Which of the following options identifies the Classes and Objects?

    • A.

      Class: Real Chocos Private Limited Objects: Chocolate

    • B.

      Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates

    • C.

      Class: Chocolate Objects: Milk Chocolates

    • D.

      Class: Fruit Chocolates Objects: Rum Chocolates

    Correct Answer
    B. Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates
    Explanation
    The correct answer is Class: Chocolate and Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates. This is because the question states that Real Chocos Private Limited manufactures three varieties of chocolates, namely Fruit Chocolates, Rum Chocolates, and Milk Chocolates. Therefore, the class would be Chocolate, which represents the general category of chocolates, and the objects would be the specific varieties of chocolates that the organization manufactures.

    Rate this question:

  • 4. 

    Consider the following code: class A { } class B extends A { } public class Code2 { public void method(A a) { System.out.println("A"); } public void method(B b) { System.out.println("B"); } public static void main(String args[]) { new Code2().method(new Object()); } } Which of the following will be the output for the above code?

    • A.

      Throws ClassCastException at runtime

    • B.

      Prints: B

    • C.

      Compilation Error 'Cannot find the symbol'

    • D.

      Prints: A

    • E.

      Return this.name.hashCode() == a.name.hashCode();

    Correct Answer
    C. Compilation Error 'Cannot find the symbol'
    Explanation
    The code will result in a compilation error because the method `method(A a)` in the `Code2` class does not have an overload that accepts an `Object` parameter. Therefore, the compiler cannot find the symbol `method(Object)`.

    Rate this question:

  • 5. 

    Consider the following code: class Planet { } class Earth extends Planet { } public class WelcomePlanet { public static void welcomePlanet(Planet planet)  { if (planet instanceof Earth) { System.out.println("Welcome!"); }  else if (planet instanceof Planet)  { System.out.println("Planet!"); } else { System.exit(0); } } public static void main(String args[])  { WelcomePlanet wp = new WelcomePlanet(); Planet planet = new Earth(); welcomePlanet(planet); } } Which of the following will be the output of the above program?

    • A.

      An exception is thrown at runtime

    • B.

      Planet!

    • C.

      The code runs with no output

    • D.

      Welcome!

    • E.

      Compilation fails

    Correct Answer
    D. Welcome!
    Explanation
    The output of the above program will be "Welcome!" because the object "planet" is an instance of the subclass Earth, which means it is also an instance of the superclass Planet. Therefore, the condition "planet instanceof Earth" will evaluate to true and the corresponding message "Welcome!" will be printed.

    Rate this question:

  • 6. 

    Consider the following code:  public class Code13 { public static void main(String... args)  { for(String s:args) System.out.print(s + ", "); System.out.println(args.length); } }  Which of the following will be the output if the above code is attempted to compile and execute?

    • A.

      Program compiles successfully and prints the passed arguments as comma separated values and finally prints the length of the arguments-list

    • B.

      Runtime Error: NoSuchMethodError

    • C.

      Variable arguments cannot be used with enhanced for-loop

    • D.

      Compilation Error: var-args cannot be used as arguments for main() method

    Correct Answer
    A. Program compiles successfully and prints the passed arguments as comma separated values and finally prints the length of the arguments-list
    Explanation
    The code is written to accept command line arguments and print them as comma separated values. It then prints the length of the arguments-list. Since the code is using the enhanced for-loop to iterate over the arguments array, it will compile successfully and produce the expected output.

    Rate this question:

  • 7. 

    Consider the following code: class UT1 { static byte m1() { final char c = 'u0001'; return c; } static byte m3(final char c) { return c; } public static void main(String[] args) { char c = 'u0003'; System.out.print(""+m1()+m3(c)); } } Which of the following gives the valid output of the above code?

    • A.

      Compile-time error

    • B.

      Prints: 13

    • C.

      Run-time error

    • D.

      Prints: 4

    • E.

      None of the listed options

    Correct Answer
    A. Compile-time error
    Explanation
    The code will result in a compile-time error because the return type of the method m1() is declared as byte, but it is trying to return a char value. The char value 'u0001' cannot be implicitly converted to a byte, hence the code will not compile.

    Rate this question:

  • 8. 

    Which are all platform independent among the following? (Choose 3)

    • A.

      JAR Files

    • B.

      Java Virtual Machine (JVM)

    • C.

      Java Development Kit (JDK)

    • D.

      Java Class Files

    • E.

      Java Source Files

    Correct Answer(s)
    A. JAR Files
    D. Java Class Files
    E. Java Source Files
    Explanation
    JAR Files, Java Class Files, and Java Source Files are all platform independent. JAR files are archives that contain compiled Java classes and resources, which can be executed on any platform that supports Java. Java Class Files are the compiled bytecode version of Java source code, which can also be executed on any platform with a Java Virtual Machine (JVM). Java Source Files are the human-readable version of Java code, which can be compiled into Java Class Files and executed on any platform with a JVM. The JVM is not platform independent itself, but it allows Java code to run on different platforms.

    Rate this question:

  • 9. 

    Consider the following partial code: public class CreditCard { private String cardID; private Integer limit; public String ownerName; public void setCardInformation(String cardID, String ownerName, Integer limit) { this.cardID = cardID; this.ownerName = ownerName; this.limit = limit; } } Which of the following statement is True regarding the above given code?

    • A.

      The class is fully encapsulated

    • B.

      The setCardInformation method breaks encapsulation

    • C.

      The code demonstrates polymorphism

    • D.

      The cardID and limit variables break polymorphism

    • E.

      The ownerName variable breaks encapsulation

    Correct Answer
    E. The ownerName variable breaks encapsulation
    Explanation
    The ownerName variable breaks encapsulation because it is declared as public, which means it can be accessed and modified directly from outside the class. Encapsulation is a principle of object-oriented programming that aims to hide the internal details of an object and only expose necessary information through methods. By declaring ownerName as public, it violates this principle and allows external code to directly manipulate the variable.

    Rate this question:

  • 10. 

    Consider the following code: 1. public class DagRag { 2. public static void main(String [] args) { 3. 4. int [][] x = new int[2][4]; 5. 6. for(int y = 0; y < 2; y++) { 7. for(int z = 0; z < 4; z++) { 8. x[y][z] = z; 9. } 10. } 11. 12. dg: for(int g = 0; g < 2; g++) { 13. rg: for(int h = 0; h < 4; h++) { 14. System.out.println(x[g][h]); 15. 16. } 17. System.out.println("The end."); 18. 19. } 20. 21. } 22. } Which of the following code snippet when inserted at lines 15 and 18 respectively, will make the above program to generate the below output? 0 1 2 3 The end.

    • A.

      If(g==3) break rg; if(h==0) break dg;

    • B.

      If(h > 3) break dg; if(g > 0) break rg;

    • C.

      If(h==3) break rg; if(g==0) break dg;

    • D.

      If(h > 3) break dg; if(g > 0) break dg;

    Correct Answer
    C. If(h==3) break rg; if(g==0) break dg;
    Explanation
    The code snippet "if(h==3) break rg; if(g==0) break dg;" will make the program generate the given output because it breaks out of the inner loop when h is equal to 3 and breaks out of the outer loop when g is equal to 0. This ensures that the inner loop iterates until h reaches 3 and the outer loop iterates until g reaches 0, allowing all the elements of the array to be printed before "The end." is printed.

    Rate this question:

  • 11. 

    Consider the following code snippet: class Animal { String name; public boolean equals(Object o) { Animal a = (Animal) o; // Code Here } } class TestAnimal { public static void main(String args[]) { Animal a = new Animal(); a.name = "Dog"; Animal b = new Animal(); b.name = "dog"; System.out.println(a.equals(b)); } } Which of the following code snippets should be replaced for the comment line (//Code Here) in the above given code, to get the output as true?

    • A.

      Return this.name.equalsIgnoreCase(a.name);

    • B.

      Return this.name.equals(a.name);

    • C.

      Return super.equals(a);

    • D.

      Return this.name == a.name;

    Correct Answer
    A. Return this.name.equalsIgnoreCase(a.name);
    Explanation
    The correct answer is "return this.name.equalsIgnoreCase(a.name);" because it compares the names of the two Animal objects, ignoring the case sensitivity. This will return true if the names are equal, regardless of whether they are in uppercase or lowercase.

    Rate this question:

  • 12. 

    Consider the following code snippet: public class TestString9 { public static void main(String st[]){ String s1 = "java"; String s2 = "java"; String s3 = "JAVA"; s2.toUpperCase(); s3.toUpperCase(); boolean b1 = s1==s2; boolean b2 = s1==s3; System.out.print(b1); System.out.print(" "+b2); } } What will be the output of the above code snippet?

    • A.

      False true

    • B.

      True true

    • C.

      True false

    • D.

      Runtime error

    • E.

      false false

    Correct Answer
    C. True false
    Explanation
    The output of the code snippet will be "true false". This is because the string objects s1 and s2 both have the same value "java", so when comparing them using the "==" operator, it will return true. However, the string object s3 has a different value "JAVA" which is in uppercase. When calling the toUpperCase() method on s3, it does not change the original string object, so s3 still remains "JAVA". Therefore, when comparing s1 and s3 using the "==" operator, it will return false.

    Rate this question:

  • 13. 

    Consider the following code snippet: public class Welcome { String title; int value; public Welcome() { title += " Planet"; } public void Welcome() { System.out.println(title + " " + value); } public Welcome(int value) { this.value = value; title = "Welcome"; Welcome(); } public static void main(String args[]) { Welcome t = new Welcome(5); } } Which of the following option will be the output for the above code snippet?

    • A.

      Compilation fails

    • B.

      Welcome Planet 5

    • C.

      Welcome 5

    • D.

      Welcome Planet

    • E.

      The code runs with no output

    Correct Answer
    C. Welcome 5
    Explanation
    The code will output "Welcome 5". This is because the constructor with the int parameter is called when creating a new instance of the Welcome class. Inside this constructor, the value variable is assigned the value 5 and the title variable is assigned the value "Welcome". Then, the Welcome() method is called, which prints out the value of the title and value variables. Since the title variable was previously assigned the value "Welcome" in the constructor, the output will be "Welcome 5".

    Rate this question:

  • 14. 

    Consider the following code: class ExampleFive { public static void main(String[] args) { final int i = 22; byte b = i; System.out.println(i + ", " + b); } } Which of the following gives the valid output for the above code?

    • A.

      Prints: 22, 20

    • B.

      Runtime Error: Cannot type cast int to byte

    • C.

      Prints: 22, 22

    • D.

      Compile Time Error: Loss of precision

    Correct Answer
    C. Prints: 22, 22
    Explanation
    The code initializes a final int variable i with a value of 22. It then tries to assign the value of i to a byte variable b. Since byte is a smaller data type than int, there is a potential loss of precision. However, in this case, the value of i (22) can be safely represented as a byte, so no compilation error occurs. Therefore, the code will print "22, 22" as the output.

    Rate this question:

  • 15. 

    Consider the following code snippet: class TestString2 { public static void main(String args[]) { String s1 = "Test1"; String s2 = "Test2"; s1.concat(s2); System.out.println(""+s1.charAt(s1.length() - 3) + s1.indexOf(s2)); } } What will be the output of the above code snippet?

    • A.

      S5

    • B.

      S-1

    • C.

      T4

    • D.

      15

    • E.

      T4

    Correct Answer
    B. S-1
    Explanation
    The code snippet creates two string objects, s1 and s2, with values "Test1" and "Test2" respectively. The concat() method is called on s1 and s2, but the result is not assigned to any variable. Therefore, the original values of s1 and s2 remain unchanged. The println() method is then called, concatenating the character at index (length - 3) of s1 with the index of s2 in s1. Since s1.concat(s2) does not modify s1, the index of s2 in s1 is -1. Therefore, the output will be "s-1".

    Rate this question:

  • 16. 

    Consider the following code: 1. public class SwitchIt { 2. public static void main(String[] args) 3. int w1 = 1; 4. int w2 = 2; 5. System.out.println(getW1W2(w1, w2)); 6. } 7. 8. public static int getW1W2(int x, int y) { 9. switch (x) { 10. case 1: x = x + y; 11. case 2: x = x + y; 12. } 13. return x; 14. } 15. } Which of the following gives the valid output of above code?

    • A.

      Compilation succeeds and the program prints "5"

    • B.

      Compilation fails because of an error on line 9

    • C.

      Compilation succeeds and the program prints "3"

    • D.

      Compilation fails because of errors on lines 10 and 11

    Correct Answer
    A. Compilation succeeds and the program prints "5"
    Explanation
    The code will compile successfully because there are no syntax errors. The program will print "5" as the output because the value of x is initially set to 1 and the switch statement will execute the case 1 and case 2 statements, adding the value of y to x twice. Therefore, x will be equal to 5 when it is returned.

    Rate this question:

  • 17. 

    Consider the following code snippet: class Train { String name = "Shatapdhi"; } class TestTrain { public static void main(String a[]) { Train t = new Train(); System.out.println(t); // Line a System.out.println(t.toString()); // Line b } } Which of the following statements are true?(Choose 3)

    • A.

      Line a prints the corresponding classname with Object's hashcode in Hexadecimal

    • B.

      Both Line a and Line b prints "Shatapdhi"

    • C.

      Both Line a and Line b will print the corresponding classname with Object's hashcode in Hexa Decimal

    • D.

      Output of Line a and Line b will be different

    • E.

      Output of Line a and Line b will be same

    Correct Answer(s)
    A. Line a prints the corresponding classname with Object's hashcode in Hexadecimal
    C. Both Line a and Line b will print the corresponding classname with Object's hashcode in Hexa Decimal
    E. Output of Line a and Line b will be same
    Explanation
    Line a prints the corresponding classname with Object's hashcode in Hexadecimal because when we print an object without explicitly calling the toString() method, it automatically calls the toString() method of the Object class. The default implementation of toString() in the Object class returns the classname followed by the hashcode of the object in hexadecimal format. Therefore, Line a prints the classname "Train" along with the hashcode of the object in hexadecimal.

    Both Line a and Line b will print the corresponding classname with Object's hashcode in Hexa Decimal because the toString() method is explicitly called in Line b. The toString() method in the Train class is not overridden, so it calls the toString() method of the Object class, which returns the classname followed by the hashcode of the object in hexadecimal format.

    The output of Line a and Line b will be the same because both statements print the classname followed by the hashcode of the object. Since the object is the same in both cases, the hashcode will be the same, resulting in the same output.

    Rate this question:

  • 18. 

    Which of the following are true about inheritance?(Choose 3)

    • A.

      In an inheritance hierarchy, a subclass can also act as a super class

    • B.

      Inheritance enables adding new features and functionality to an existing class without modifying the existing class

    • C.

      Inheritance enables adding new features and functionality to an existing class without modifying the existing class

    • D.

      Inheritance is a kind of Encapsulation

    • E.

      Inheritance does not allow sharing data and methods among multiple classes

    Correct Answer(s)
    A. In an inheritance hierarchy, a subclass can also act as a super class
    B. Inheritance enables adding new features and functionality to an existing class without modifying the existing class
    C. Inheritance enables adding new features and functionality to an existing class without modifying the existing class
    Explanation
    Inheritance allows a subclass to inherit the properties and methods of its superclass, which means that a subclass can also act as a superclass in an inheritance hierarchy. This allows for code reuse and promotes modularity. Additionally, inheritance enables adding new features and functionality to an existing class without modifying the existing class, which helps to maintain the integrity of the original class and simplifies the process of extending its capabilities.

    Rate this question:

  • 19. 

    Consider the following code: 1 public class A { 2 public void m1() {System.out.print("A.m1, ");} 3 protected void m2() {System.out.print("A.m2, ");} 4 private void m3() {System.out.print("A.m3, ");} 5 void m4() {System.out.print("A.m4, ");} 6 7 public static void main(String[] args) { 8 A a = new A(); 9 a.m1(); 10 a.m2(); 11 a.m3(); 12 a.m4(); 13 } 14 } Which of the following gives the lines that need to be removed in order to compile and run the above code correctly?

    • A.

      Lines 10, 11

    • B.

      No need to comment any line. Will compile and run

    • C.

      Lines 10, 11 and 12

    • D.

      Line 11

    Correct Answer
    B. No need to comment any line. Will compile and run
    Explanation
    The code will compile and run correctly without the need to comment any line. All the methods called in the main method (m1, m2, m3, m4) are accessible within the class A. The access modifiers (public, protected, private) do not affect the ability to call these methods within the same class. Therefore, no lines need to be removed for the code to compile and run correctly.

    Rate this question:

  • 20. 

    Which of the following options gives the difference between == operator and equals() method?

    • A.

      Equals compares hash value and == compares character sequence

    • B.

      No difference;they are essentially the same

    • C.

      If equals() is true then == is also true

    • D.

      ==compares object's memory address but equals character sequence

    • E.

      == works on numbers equals() works on characters

    Correct Answer
    D. ==compares object's memory address but equals character sequence
    Explanation
    The correct answer is that the == operator compares the memory address of objects, while the equals() method compares the character sequence of objects.

    Rate this question:

  • 21. 

    Which of the following classes is new to JDK 1.6?

    • A.

      Java.io.File

    • B.

      Java.io.Serializable

    • C.

      Java.io.FileFilter

    • D.

      Java.io.Console

    • E.

      Java.io.Externalizable

    Correct Answer
    D. Java.io.Console
    Explanation
    The class "java.io.Console" is new to JDK 1.6. This class provides a way to interact with the user through the command line, allowing input and output operations. It was introduced in JDK 1.6 to enhance the capabilities of command line applications in Java.

    Rate this question:

  • 22. 

    Which of the following statements are correct regarding Static Blocks?(Choose 3)

    • A.

      A class that have static block, should have the main() method defined in it

    • B.

      A class can have more than one static block

    • C.

      Static blocks are executed only once

    • D.

      Static blocks are executed before main() method

    • E.

      A static block can call other methods in a class

    Correct Answer(s)
    B. A class can have more than one static block
    C. Static blocks are executed only once
    D. Static blocks are executed before main() method
    Explanation
    A class can have more than one static block: This statement is correct because a class can have multiple static blocks, which are used to initialize static variables or perform other tasks before the class is used.

    Static blocks are executed only once: This statement is correct because static blocks are executed only once when the class is loaded into memory. They are not executed again even if the class is instantiated multiple times.

    Static blocks are executed before the main() method: This statement is correct because static blocks are executed in the order they appear in the code, before the main() method is executed. This allows any necessary initialization to be performed before the main() method starts executing.

    Therefore, the correct statements regarding static blocks are that a class can have more than one static block, static blocks are executed only once, and static blocks are executed before the main() method.

    Rate this question:

  • 23. 

    Which of the following flow control features does Java support? (Choose 2)

    • A.

      Labeled continue

    • B.

      Labeled goto

    • C.

      Labeled throw

    • D.

      Labeled catch

    • E.

      Labeled break

    Correct Answer(s)
    A. Labeled continue
    E. Labeled break
    Explanation
    Java supports the flow control features of labeled continue and labeled break. Labeled continue is used to skip the current iteration of a loop and move to the next iteration, while labeled break is used to exit a loop or switch statement. Labeled goto, labeled throw, and labeled catch are not supported by Java.

    Rate this question:

  • 24. 

    Which of the following options gives the relationship between a Spreadsheet Object and Cell Objects?

    • A.

      Polymorphism

    • B.

      Association

    • C.

      Aggregation

    • D.

      Inheritance

    • E.

      Persistence

    Correct Answer
    C. Aggregation
    Explanation
    Aggregation is the correct answer because it represents a relationship between objects where one object is composed of or contains other objects. In the context of a spreadsheet, a Spreadsheet Object can be composed of or contain multiple Cell Objects. This relationship allows for the organization and manipulation of data within the spreadsheet.

    Rate this question:

  • 25. 

    Consider the following program: public class D extends Thread { public void run() { System.out.println("Before start method"); this.stop(); System.out.println("After stop method"); } public static void main(String[] args) { D a = new D(); a.start(); } } What will be the output of the above program?

    • A.

      'Before start method' and 'After stop method'

    • B.

      Compilation error

    • C.

      'Before start method' only

    • D.

      Runtime exception

    Correct Answer
    C. 'Before start method' only
    Explanation
    The program will output 'Before start method' only. This is because the stop() method is called immediately after the start() method, which causes the thread to stop before it has a chance to execute the rest of the code in the run() method. Therefore, the "After stop method" print statement is never reached.

    Rate this question:

  • 26. 

    Consider the following code: public class Choco { Choco() { System.out.print("Choco"); } class Bar { Bar() { System.out.print("bar"); } public void go() { System.out.print("sweet"); } } public static void main(String[] args) { Choco c = new Choco(); c.makeBar(); } void makeBar(){ // Insert code here } } Which of the following code snippet when substituted individually to the above commented line (// Insert code here) will give the following output?

    • A.

      New Choco().go();

    • B.

      New Bar().go();

    • C.

      New Choco(). new Bar().go();

    • D.

      (new Bar() {}).go();

    • E.

      Go();

    Correct Answer
    D. (new Bar() {}).go();
    Explanation
    The correct answer is "(new Bar() {}).go();". This code creates a new instance of the Bar class using an anonymous inner class and then calls the go() method on that instance. The output will be "barsweet".

    Rate this question:

  • 27. 

    Consider the following code: public class SwitchIt { public static void main(String args[]) { int x = 10; switch(x) { case 10: for(int i=0; i break; case 20: System.out.println(x); break; case 30: System.out.println(x*2); break; default: System.out.println(x*3); } } } Which of the following will be the output for the above program?

    • A.

      10

    • B.

      11

    • C.

      No output

    • D.

      30

    Correct Answer
    A. 10
    Explanation
    The output for the above program will be 10. This is because the value of x is 10, which matches the case 10 in the switch statement. Therefore, the code inside the case 10 block will be executed, which includes the statement System.out.println(x). This will print the value of x, which is 10, as the output.

    Rate this question:

  • 28. 

    Consider the following code: class One { public One() { System.out.print(1); } } class Two extends One { public Two() { System.out.print(2); } } class Three extends Two { public Three() { System.out.print(3); } } public class Numbers { public static void main(String[] argv) { new Three(); } } Which of the following will be the output for the above program?

    • A.

      321

    • B.

      No output

    • C.

      123

    • D.

      32

    • E.

      3

    Correct Answer
    C. 123
    Explanation
    The code defines three classes: One, Two, and Three. Each class has a constructor that prints a number. Class Three extends class Two, which extends class One. The main method creates an instance of class Three. When the instance is created, the constructors of all three classes are called in order. Therefore, the output will be 123.

    Rate this question:

  • 29. 

    Consider the following code: public class TestOne { public static void main(String args[]) { byte x = 3; byte y = 5; System.out.print((y%x) + ", "); System.out.println(y == ((y/x) *x +(y%x))); } } Which of the following gives the valid output for above?

    • A.

      Prints: 1, true

    • B.

      Prints 2, false

    • C.

      Prints: 1, false

    • D.

      Prints: 2, true

    Correct Answer
    D. Prints: 2, true
    Explanation
    The code first calculates the remainder of y divided by x using the modulus operator (%), which is 5 % 3 = 2. It then prints this value, followed by a comma.

    Next, it checks if y is equal to the result of ((y/x) * x + (y%x)). Since y is 5, and ((y/x) * x + (y%x)) simplifies to ((5/3) * 3 + (5%3)) = (1 * 3 + 2) = 5, the condition y == ((y/x) * x + (y%x)) evaluates to true.

    Therefore, the code prints "2, true".

    Rate this question:

  • 30. 

    Consider the following code: 1. public class Garment { 2. public enum Color { 3. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 4. private final int rgb; 5. Color( int rgb) { this.rgb = rgb; } 6. public int getRGB() { return rgb; } 7. }; 8. public static void main( String[] argv) { 9. // insert code here 10. } 11.} Which of the following code snippets, when inserted independently at line 9, allow the Garment class to compile? (Choose 2)

    • A.

      Color treeColor = Color.GREEN;

    • B.

      Color purple = new Color( 0xff00ff);

    • C.

      Color skyColor = BLUE;

    • D.

      If( RED.getRGB() < BLUE.getRGB() ) {}

    • E.

      If( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}

    Correct Answer(s)
    A. Color treeColor = Color.GREEN;
    E. If( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}
    Explanation
    The code snippet "Color treeColor = Color.GREEN;" allows the Garment class to compile because it creates a new variable "treeColor" of type Color and assigns it the value of Color.GREEN, which is one of the enum constants defined in the Color enum.

    The code snippet "if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}" allows the Garment class to compile because it compares the ordinal values of the enum constants Color.RED and Color.BLUE, which are integers, using the "

    Rate this question:

  • 31. 

    Consider the following code: class Alpha { protected Beta b; } class Gamma extends Alpha { } class Beta { } Which of the following statement is True?

    • A.

      Beta has-a Gamma and Gamma is-a Alpha.

    • B.

      Gamma has-a Beta and Gamma is-a Alpha

    • C.

      Alpha is-a Gamma and has-a Beta.

    • D.

      Gamma is-a Beta and has-a Alpha.

    • E.

      Alpha has-a Beta and Alpha is-a Gamma

    Correct Answer
    B. Gamma has-a Beta and Gamma is-a Alpha
    Explanation
    In the given code, class Gamma extends class Alpha, which means Gamma is a type of Alpha. Therefore, the statement "Gamma is-a Alpha" is true. Additionally, the code also declares a protected variable of type Beta in class Alpha, which means Gamma has a Beta. Therefore, the statement "Gamma has-a Beta" is also true.

    Rate this question:

  • 32. 

    Consider the following code snippet:   String deepak = "Did Deepak see bees? Deepak did.";   Which of the following method calls would refer to the letter b in the string referred by the variable deepak?

    • A.

      CharAt(13)

    • B.

      CharAt(15)

    • C.

      CharAt(12)

    • D.

      CharAt(16)

    • E.

      CharAt(14)

    Correct Answer
    B. CharAt(15)
    Explanation
    The method call charAt(15) refers to the letter "b" in the string "Did Deepak see bees? Deepak did.". The index of the character "b" in the string is 15, as indexing starts from 0. Therefore, calling the charAt(15) method will return the character "b".

    Rate this question:

  • 33. 

    Which of the following codes will compile and run properly?

    • A.

      Public class Test2 { static public void main(String[] in) { System.out.println("Test2"); } }

    • B.

      Public class Test4 { static int main(String args[]) { System.out.println("Test4"); } }

    • C.

      public class Test1 { public static void main() { System.out.println("Test1"); } }

    • D.

      Public class Test3 { public static void main(String args) { System.out.println("Test3"); } }

    • E.

      Public class Test5 { static void main(String[] data) { System.out.println("Test5"); } }

    Correct Answer
    A. Public class Test2 { static public void main(String[] in) { System.out.println("Test2"); } }
    Explanation
    The correct answer is public class Test2 { static public void main(String[] in) { System.out.println("Test2"); } } because it follows the correct syntax for the main method in Java. The main method must be declared as public, static, and void, and it must accept an array of strings as a parameter. Additionally, the class name must match the file name, and the code inside the main method will be executed when the program is run.

    Rate this question:

  • 34. 

    Consider the following code: 1. public class Circle1 { 2. private String string = "String1"; 3. void work() { 4. String x = "String2"; 5. class Circle2 { 6. public void peepOut() { 7. System.out.println(string); 8. System.out.println(x); 9. } 10. } 11. new Circle2().peepOut(); 12. } 13. 14. public static void main(String args[]) { 15. Circle1 c1 = new Circle1(); 16. c1.work(); 17. } 18. } Which of the following changes made to the above code will make the code to compile and execute properly and gives the following output? String1 String2

    • A.

      The variable at line 4 should be declared as final

    • B.

      The variable at line 2 should be declared as final

    • C.

      The method at line 6 should be defined as final method

    • D.

      The inner class Circle 2 should be an abstract class

    • E.

      The object for the inner class Circle2 should be created in main() method

    Correct Answer
    A. The variable at line 4 should be declared as final
    Explanation
    The variable at line 4 should be declared as final because it is being accessed by the inner class Circle2. In order for the inner class to access the variable, it needs to be declared as final to ensure that its value does not change.

    Rate this question:

  • 35. 

    Which of the following options gives the relationship between a Pilot class and Plane class?

    • A.

      Inheritance

    • B.

      Polymorphism

    • C.

      Persistence

    • D.

      Aggregation

    • E.

      Association

    Correct Answer
    E. Association
    Explanation
    Association is a relationship between two classes where one class is connected to another class, but there is no ownership or dependency between them. In the given question, the relationship between a Pilot class and Plane class can be an association, as pilots can be associated with planes without any ownership or dependency.

    Rate this question:

  • 36. 

    Consider the following code: public class Key1 { public boolean testAns( String ans, int n ) { boolean rslt; if (ans.equalsIgnoreCase("YES") & n > 5) rslt = true; return rslt; } public static void main(String args[]) { System.out.println(new Key1().testAns("no", 5)); } } Which of the following will be the output of the above program?

    • A.

      Compile-time error

    • B.

      Runtime Error

    • C.

      NO

    • D.

      False

    • E.

      True

    Correct Answer
    A. Compile-time error
    Explanation
    The code will result in a compile-time error because the variable "rslt" is not initialized before it is returned in the method "testAns".

    Rate this question:

  • 37. 

    Which of the following methods are defined in Object class? (Choose 3)

    • A.

      ToString()

    • B.

      CompareTo(Object)

    • C.

      HashCode()

    • D.

      Equals(Object)

    • E.

      Run()

    Correct Answer(s)
    A. ToString()
    C. HashCode()
    D. Equals(Object)
    Explanation
    The methods toString(), hashCode(), and equals(Object) are defined in the Object class. The toString() method returns a string representation of an object, the hashCode() method returns the hash code value for an object, and the equals(Object) method checks if two objects are equal. These methods are inherited by all classes in Java as the Object class is the root class for all classes in Java. The run() and compareTo(Object) methods are not defined in the Object class.

    Rate this question:

  • 38. 

    Consider the following code: public class Code4 { private int second = first; private int first = 1000; public static void main(String args[]) { System.out.println(new Code4().second); } } Which of the following will be the output for the above code?

    • A.

      Throws a Runtime error 'Illegal forward reference'

    • B.

      1000

    • C.

      Compiler complains about forward referencing of member variables first and second

    • D.

      Compiler complains about private memebers is not accessible from main() method

    Correct Answer
    C. Compiler complains about forward referencing of member variables first and second
    Explanation
    The code is trying to assign the value of the variable "first" to the variable "second" before "first" is actually declared. This is known as forward referencing, and it is not allowed in Java. Therefore, the compiler will complain about this error.

    Rate this question:

  • 39. 

    Consider the following code: public class ObParam{ public int b = 20; public static void main(String argv[]){ ObParam o = new ObParam(); methodA(o); } public static void methodA(ObParam a) { a.b++; System.out.println(a.b); methodB(a); System.out.println(a.b); } public void methodB(ObParam b) { b.b--; } } Which of the following gives the correct output for the above code?

    • A.

      Prints: 21 21

    • B.

      Prints: 20 21

    • C.

      Compilation Error: Non-static method methodB() cannot be referenced from static context methodA()

    • D.

      Prints: 20 20

    • E.

      Prints: 21 20

    Correct Answer
    C. Compilation Error: Non-static method methodB() cannot be referenced from static context methodA()
    Explanation
    The correct answer is "Compilation Error: Non-static method methodB() cannot be referenced from static context methodA()". This is because the method methodB() is not declared as static, so it can only be accessed through an instance of the class. However, methodA() is a static method and does not have an instance of the class, so it cannot access the non-static method methodB(). This results in a compilation error.

    Rate this question:

  • 40. 

    Which of the following statements are true? (Choose 2)

    • A.

      All exceptions are thrown programmatically from the code or API

    • B.

      All exceptions are thrown by JVM

    • C.

      JVM cannot throw user-defined exceptions

    • D.

      JVM thrown exceptions can be thrown programmatically

    • E.

      All RuntimeException are thrown by JVM

    Correct Answer(s)
    C. JVM cannot throw user-defined exceptions
    D. JVM thrown exceptions can be thrown programmatically
    Explanation
    The first statement, "All exceptions are thrown programmatically from the code or API," is false. Exceptions can be thrown programmatically, but they can also be thrown automatically by the JVM or other system components.

    The second statement, "All exceptions are thrown by JVM," is false. While the JVM does throw some exceptions, not all exceptions are thrown by the JVM. Exceptions can also be thrown programmatically by the code or API.

    The third statement, "JVM cannot throw user-defined exceptions," is true. User-defined exceptions are exceptions that are created by the programmer, and the JVM does not throw these types of exceptions.

    The fourth statement, "JVM thrown exceptions can be thrown programmatically," is true. The JVM can throw exceptions, and these exceptions can also be thrown programmatically by the code or API.

    The fifth statement, "All RuntimeException are thrown by JVM," is false. While the JVM does throw some RuntimeExceptions, not all RuntimeExceptions are thrown by the JVM. RuntimeExceptions can also be thrown programmatically by the code or API.

    Rate this question:

  • 41. 

    Delimiters themselves be considered as tokens. State True or False.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Delimiters themselves cannot be considered as tokens. Delimiters are characters used to separate or mark the boundaries of different tokens in a given text. Tokens, on the other hand, are meaningful units of information, such as words or symbols, that are extracted from the text. Delimiters serve as separators between tokens, but they are not considered tokens themselves. Therefore, the statement is false.

    Rate this question:

  • 42. 

    Consider the following code: public class ExampleSeven { public static void main(String [] args) { String[] y = new String[1]; String x = "hello"; y[0] = x; // Code here System.out.println("match"); } else { System.out.println("no match"); } } } Which of the following code snippet when substituted at the commented line (// Code here) in the above code will make the program to print "no match"?

    • A.

      If (x != y[0].toString()) {

    • B.

      If (x & y[0]) {

    • C.

      If (x.equals(y[0])) {

    • D.

      If (!x.equals(y[0])) {

    Correct Answer
    D. If (!x.equals(y[0])) {
    Explanation
    The correct answer is "if (!x.equals(y[0])) {". This code snippet uses the "equals" method to compare the value of x and y[0]. The "!" operator negates the result of the comparison, so if x and y[0] are not equal, the condition will be true and "no match" will be printed.

    Rate this question:

  • 43. 

    Consider the following code: class Animal { public String noise() { return "noise"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return "meow"; } } class MakeNoise { public static void main(String args[]) { Animal animal = new Dog(); Cat cat = (Cat)animal; System.out.println(cat.noise()); } } Which of the following option will be the output of the above code snippet?

    • A.

      Noise

    • B.

      Bark

    • C.

      Meow

    • D.

      Compilation fails

    • E.

      An exception is thrown at runtime

    Correct Answer
    E. An exception is thrown at runtime
    Explanation
    The code snippet tries to cast an instance of the Dog class to the Cat class. However, since a Dog is not a subclass of Cat, a ClassCastException will be thrown at runtime.

    Rate this question:

  • 44. 

    Consider the following code: public class GetArray { public static void main(String args[]) { float invt[][]; float[] prct, grts[]; float[][] sms, hms[]; (// Insert statement1 here) (// Insert statement2 here) (// Insert statement3 here) } } Which of the following listed statements can be inserted at the above commented lines (// Insert statement1 here, // Insert statement2 here, // Insert statement3 here) to make the program to compile without errors? (Choose 3)

    • A.

      Grts = new float[1][4];

    • B.

      Invt = grts;

    • C.

      Hms = new float[2][5];

    • D.

      Invt = new float[4][2];

    • E.

      Grts = new float[1];

    Correct Answer(s)
    A. Grts = new float[1][4];
    B. Invt = grts;
    D. Invt = new float[4][2];
    Explanation
    The first statement "grts = new float[1][4];" creates a 2-dimensional float array with 1 row and 4 columns and assigns it to the variable "grts". This statement is valid and does not cause any compilation errors.

    The second statement "invt = grts;" assigns the value of "grts" to the variable "invt". Since both variables are of the same type (2-dimensional float array), this assignment is valid and does not cause any compilation errors.

    The third statement "invt = new float[4][2];" creates a new 2-dimensional float array with 4 rows and 2 columns and assigns it to the variable "invt". This statement is also valid and does not cause any compilation errors.

    Therefore, the three listed statements can be inserted at the commented lines to make the program compile without errors.

    Rate this question:

  • 45. 

    Which of the following statements are correct regarding Instance Block?(Choose 3)

    • A.

      A class can have more than one instance block

    • B.

      An instance block cannot initialise the class members

    • C.

      Instance blocks are executed only when the instances are created from main() method of that class

    • D.

      Instance blocks are executed before constructors

    • E.

      Instance blocks are executed for every created instance

    Correct Answer(s)
    A. A class can have more than one instance block
    D. Instance blocks are executed before constructors
    E. Instance blocks are executed for every created instance
    Explanation
    A class can have more than one instance block: This statement is correct because a class can have multiple instance blocks, which are used to initialize instance variables or perform other actions before the object is created.

    Instance blocks are executed before constructors: This statement is correct because instance blocks are executed before constructors during the object creation process. Instance blocks are used to initialize instance variables, and they are executed before any constructor code is executed.

    Instance blocks are executed for every created instance: This statement is correct because instance blocks are executed every time a new instance of the class is created. This ensures that the necessary initialization or actions are performed for each individual instance.

    Rate this question:

Quiz Review Timeline +

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

  • Current Version
  • Sep 05, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 14, 2011
    Quiz Created by
    Leonfernando
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.