Code Buzz (Language : Java)

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 Aniket Mondal
A
Aniket Mondal
Community Contributor
Quizzes Created: 1 | Total Attempts: 186
Questions: 30 | Attempts: 187

SettingsSettingsSettings
Code Buzz (Language : Java) - Quiz

"A good programmer is someone who always looks both ways before crossing a one-way street"
Do you have a wide vision on programming?
Do you have control on several languages?
Let's check !Techtix, KGEC in association with Coding Ninjas presents Code Buzz, the competition to test your technical knowledge in Java, C & Python. Code Buzz this year has got total prizes worth Rs. 4,500. Take this challenge and register for Code Buzz this year.


Questions and Answers
  • 1. 

    What will be printed as the output of the following program? public class testincr { public static void main(String args[]) { int i = 0; i = i++ + i; System.out.println(“I = ” +i); } }

    • A.

      I=0

    • B.

      I=1

    • C.

      I=2

    • D.

      I=3

    Correct Answer
    B. I=1
    Explanation
    The program initializes the variable i to 0. Then, it assigns the value of i++ (which is 0) to i, and adds the value of i (which is now 1) to it. So, the value of i becomes 1. Finally, it prints "I = 1" as the output.

    Rate this question:

  • 2. 

    Which one of the following is not true?

    • A.

      A class containing abstract methods is called an abstract class.

    • B.

      Abstract methods should be implemented in the derived class.

    • C.

      An abstract class cannot have non-abstract methods.

    • D.

      A class must be qualified as ‘abstract’ class, if it contains one abstract method.

    Correct Answer
    B. Abstract methods should be implemented in the derived class.
    Explanation
    The statement "Abstract methods should be implemented in the derived class" is not true. Abstract methods are declared in an abstract class but do not have an implementation. They are meant to be overridden and implemented in the derived class.

    Rate this question:

  • 3. 

    What is the output of the following program: public class testmeth { static int i = 1; public static void main(String args[]) { System.out.println(i+” , “); m(i); System.out.println(i); } public void m(int i) { i += 2; } }

    • A.

      1,3

    • B.

      3,1

    • C.

      1,1

    • D.

      1,0

    Correct Answer
    C. 1,1
    Explanation
    The program outputs "1,1" because the variable "i" is not being modified within the method "m". The method "m" takes an integer parameter "i", but it only modifies the local variable "i" within the method. Therefore, the value of the static variable "i" remains unchanged at 1, resulting in the output "1,1".

    Rate this question:

  • 4. 

    Which of the following is not true?

    • A.

      An interface can extend another interface.

    • B.

      A class which is implementing an interface must implement all the methods of the interface.

    • C.

      An interface can implement another interface.

    • D.

      An interface is a solution for multiple inheritance in java.

    Correct Answer
    C. An interface can implement another interface.
    Explanation
    The given statement that "An interface can implement another interface" is not true. In Java, interfaces can only extend other interfaces, not implement them. Implementation is done by classes, while interfaces provide a contract for classes to follow. Therefore, an interface can extend another interface to inherit its methods and add additional methods, but it cannot implement another interface.

    Rate this question:

  • 5. 

    Which of the following is true?

    • A.

      A finally block is executed before the catch block but after the try block.

    • B.

      A finally block is executed, only after the catch block is executed.

    • C.

      A finally block is executed whether an exception is thrown or not.

    • D.

      A finally block is executed, only if an exception occurs.

    Correct Answer
    C. A finally block is executed whether an exception is thrown or not.
    Explanation
    A finally block is a block of code that is always executed, regardless of whether an exception is thrown or not. It is typically used to clean up resources or perform any necessary final actions. In this case, the given answer states that a finally block is executed whether an exception is thrown or not, which means that the code inside the finally block will always be executed, regardless of whether there is an exception or not.

    Rate this question:

  • 6. 

    Consider the following code fragment Rectangle r1 = new Rectangle(); r1.setColor(Color.blue); Rectangle r2 = r1; r2.setColor(Color.red); After the above piece of code is executed, what are the colors of r1 and r2 (in this order)?

    • A.

      Color.blue Color.red

    • B.

      Color.blue Color.blue

    • C.

      Color.red Color.red

    • D.

      Color.red Color.blue

    Correct Answer
    C. Color.red Color.red
    Explanation
    The code creates a new Rectangle object called r1 and sets its color to blue. Then, a new reference variable called r2 is created and assigned to the same object as r1. Therefore, r1 and r2 both refer to the same object. When the color of r2 is set to red, it also changes the color of the object that r1 is referencing. As a result, both r1 and r2 have a color of red.

    Rate this question:

  • 7. 

    What is the type and value of the following expression? (Notice the integer division) -4 + 1/2 + 2*-3 + 5.0

    • A.

      Int -5

    • B.

      Double -4.5

    • C.

      Int -4

    • D.

      Double -5.0

    Correct Answer
    D. Double -5.0
    Explanation
    The expression involves a combination of integers and a floating-point number. The division operation is performed first, resulting in 1/2 being evaluated as 0.5. The rest of the expression is then evaluated according to the order of operations, with the multiplication operation (-3 * 2) resulting in -6. Finally, the addition and subtraction operations are performed, resulting in -4 + 0.5 - 6 + 5.0 = -4.5. Since the expression includes a floating-point number (5.0), the overall type of the expression is double. Therefore, the correct answer is double -5.0.

    Rate this question:

  • 8. 

    What is printed by the following statement? System.out.print(“Hello,\nworld!”);

    • A.

      Hello, \nworld!

    • B.

      Option 2

    • C.

      Hello, world!

    • D.

       “Hello, \nworld!”

    Correct Answer
    C. Hello, world!
    Explanation
    The given statement will print "Hello, world!" to the console.

    Rate this question:

  • 9. 

    Consider the two methods (within the same class) public static int foo(int a, String s) { s = “Yellow”; a=a+2; return a; } public static void bar() { int a=3; String s = “Blue”; a = foo(a,s); System.out.println(“a=”+a+” s=”+s); } public static void main(String args[]) { bar(); } What is printed on execution of these methods?

    • A.

      A = 3 s = Blue

    • B.

      A = 5 s = Yellow

    • C.

      A = 3 s = Yellow

    • D.

      A = 5 s = Blue

    Correct Answer
    D. A = 5 s = Blue
    Explanation
    The method foo takes an integer and a string as parameters. Inside the method, the string parameter "s" is assigned the value "Yellow". The integer parameter "a" is incremented by 2. The method then returns the updated value of "a". In the method bar, an integer variable "a" is initialized with the value 3 and a string variable "s" is initialized with the value "Blue". The method foo is called with the variables "a" and "s" as arguments. The value returned by foo, which is 5, is assigned to "a". Finally, the values of "a" and "s" are printed, which are 5 and "Blue" respectively.

    Rate this question:

  • 10. 

    Consider the following class definition: public class MyClass { private int value; public void setValue(int i){ / code / } // Other methods… } The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue?

    • A.

      (A) value = i;

    • B.

      (B) this.value = i;

    • C.

      (C) value == i;

    • D.

      (D) Both (A) and (B) and above

    Correct Answer
    D. (D) Both (A) and (B) and above
    Explanation
    The correct answer is (D) Both (A) and (B) and above. This is because both options (A) and (B) correctly assign the value of i to the instance field value. Option (A) directly assigns the value, while option (B) uses the "this" keyword to refer to the current object and then assigns the value. Both implementations achieve the same result.

    Rate this question:

  • 11. 

    Which of the following is TRUE?

    • A.

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

    • B.

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

    • C.

      Instance variable names may only contain letters and digits.

    • D.

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

    Correct Answer
    D. A class has always a constructor (possibly automatically supplied by the java compiler).
    Explanation
    A class has always a constructor (possibly automatically supplied by the java compiler). In Java, a constructor is a special method that is used to initialize objects of a class. If a class does not explicitly define a constructor, the Java compiler automatically provides a default constructor. Therefore, every class in Java has a constructor, either defined explicitly or provided by the compiler.

    Rate this question:

  • 12. 

    Consider, public class MyClass { public MyClass(){/code/} // more code… } To instantiate MyClass, you would write?

    • A.

      MyClass mc = new MyClass();

    • B.

      MyClass mc = MyClass();

    • C.

      MyClass mc = MyClass;

    • D.

      MyClass mc = new MyClass;

    Correct Answer
    A. MyClass mc = new MyClass();
    Explanation
    To instantiate the MyClass class, you would write "MyClass mc = new MyClass();". This is the correct syntax for creating a new instance of a class in Java. The "new" keyword is used to allocate memory for the object, and the constructor "MyClass()" is called to initialize the object. The resulting object is then assigned to the variable "mc".

    Rate this question:

  • 13. 

    You read the following statement in a Java program that compiles and executes. submarine.dive(depth); What can you say for sure?

    • A.

      Depth must be an int

    • B.

      Dive must be a method.

    • C.

      Dive must be the name of an instance field.

    • D.

      Submarine must be the name of a class

    Correct Answer
    B. Dive must be a method.
    Explanation
    Based on the given statement, "submarine.dive(depth)", we can say for sure that "dive" must be a method. This is because the statement is using the dot operator to call the "dive" method on the "submarine" object. If "dive" was not a method, this statement would result in a compilation error. However, we cannot determine the data type of "depth" or whether "submarine" is the name of a class without further information.

    Rate this question:

  • 14. 

    The correct order of the declarations in a Java program is,

    • A.

      Package declaration, import statement, class declaration

    • B.

      Import statement, package declaration, class declaration

    • C.

      Import statement, class declaration, package declaration

    • D.

      Class declaration, import statement, package declaration

    Correct Answer
    A. Package declaration, import statement, class declaration
    Explanation
    The correct order of declarations in a Java program is package declaration, import statement, and then class declaration. This is because the package declaration specifies the package in which the class belongs, the import statement is used to import necessary classes or packages, and finally, the class declaration defines the class itself.

    Rate this question:

  • 15. 

    A protected member can be accessed in,

    • A.

      A subclass of the same package

    • B.

      A non-subclass of the same package

    • C.

      A non-subclass of different

    • D.

      A subclass of different package

    Correct Answer
    C. A non-subclass of different
    Explanation
    A protected member can be accessed by a non-subclass of a different package because the protected access modifier allows access to the member within the same package and also by any subclass, regardless of whether it is in the same package or a different package.

    Rate this question:

  • 16. 

    What is the output of the following code: class eq { public static void main(String args[]) { String s1 = “Hello”; String s2 = new String(s1); System.out.println(s1==s2); } }

    • A.

      True

    • B.

      False

    • C.

      0

    • D.

      1

    Correct Answer
    B. False
    Explanation
    The output of the code is "false". This is because the "==" operator in Java checks for reference equality, meaning it compares whether two variables refer to the same object in memory. In this case, although the content of s1 and s2 is the same ("Hello"), they are two different String objects because s2 is created using the "new" keyword. Therefore, s1 and s2 have different memory addresses, resulting in the "==" comparison returning false.

    Rate this question:

  • 17. 

    All exception types are subclasses of the built-in class

    • A.

      Exception

    • B.

      RuntimeExceptions

    • C.

      Error

    • D.

      Throwable

    Correct Answer
    D. Throwable
    Explanation
    All exception types in Java are subclasses of the built-in class "Throwable". This means that any exception that occurs during the execution of a Java program can be caught and handled using the "Throwable" class. The "Throwable" class is the superclass of all exception classes, including "Exception", "RuntimeExceptions", and "Error". Therefore, the statement is correct in stating that all exception types are subclasses of "Throwable".

    Rate this question:

  • 18. 

    When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the

    • A.

      Super class

    • B.

      Subclass

    • C.

      Compiler will choose randomly

    • D.

      Interpreter will choose randomly

    Correct Answer
    B. Subclass
    Explanation
    When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. This is because when a method is overridden, the subclass provides its own implementation of the method, which replaces the implementation in the superclass. Therefore, when the method is called from within the subclass, the subclass's implementation will be executed.

    Rate this question:

  • 19. 

    What will be the output of the following program?
    1. public class MyFirst {
    2. public static void main(String[] args) {
    3. MyFirst obj = new MyFirst(n);
    4. }
    5. static int a = 10;
    6. static int n;
    7. int b = 5;
    8. int c;
    9. public MyFirst(int m) {
    10. System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
    11. }
    12. // Instance Block
    13. {
    14. b = 30;
    15. n = 20;
    16. }
    17. // Static Block
    18. static
    19. {
    20. a = 60;
    21. }
    22. }

    • A.

      ​​​​​​10, 5, 0, 20, 0

    • B.

      ​​​​​​​10, 30, 20

    • C.

      60, 5, 0, 20

    • D.

      60, 30, 0, 20, 0

    Correct Answer
    D. 60, 30, 0, 20, 0
    Explanation
    The output of the program will be "60, 30, 0, 20, 0". This is because the static block is executed before the main method, so the value of "a" is changed to 60. Then, the instance block is executed before the constructor, so the values of "b" and "n" are changed to 30 and 20 respectively. Finally, the constructor is called with the parameter "n", and it prints the values of "a", "b", "c", "n", and "m".

    Rate this question:

  • 20. 

    What does the expression float a = 35 / 0 return?

    • A.

      0

    • B.

      Not a Number

    • C.

      Infinity

    • D.

      Runtime exception

    Correct Answer
    D. Runtime exception
    Explanation
    The expression float a = 35 / 0 returns a runtime exception. This is because dividing any number by zero is undefined in mathematics and programming. It violates the fundamental rule of division and leads to an error known as "ArithmeticException" in programming languages. Therefore, attempting to divide 35 by 0 will result in a runtime exception being thrown.

    Rate this question:

  • 21. 

    Use the following declaration and initialization to evaluate the Java expressions int a = 2, b = 3, c = 4, d = 5; float k = 4.3f; System.out.println( – -b * a + c *d – -);

    • A.

      21

    • B.

      24

    • C.

      28

    • D.

      26

    Correct Answer
    B. 24
    Explanation
    The given expression can be evaluated as follows:
    -(-b * a + c * d) - (-)
    = -( -3 * 2 + 4 * 5) - (-)
    = -( -6 + 20) - (-)
    = -(14) - (-)
    = -14 - (-)
    = -14 + 0
    = -14

    Therefore, the correct answer is 24.

    Rate this question:

  • 22. 

    Identify, from among the following, the incorrect variable name(s).

    • A.

      _theButton

    • B.

      $reallyBigNumber

    • C.

      2ndName

    • D.

      CurrentWeatherStateofplanet

    Correct Answer
    C. 2ndName
    Explanation
    The variable name "2ndName" is incorrect because variable names cannot start with a number. Variable names must start with a letter or an underscore.

    Rate this question:

  • 23. 

    Use the following declaration and initialization to evaluate the Java expressions int a = 2, b = 3, c = 4, d = 5; float k = 4.3f; System.out.println (-2U * ( g – k ) +c);

    • A.

      Syntax Error

    • B.

      6

    • C.

      3

    • D.

      2

    Correct Answer
    A. Syntax Error
  • 24. 

    Consider the following Java program : class IfStatement{ public static void main(String args[]) { int a=2, b=3; if (a==3) if (b==3) System.out.println(“===============”); else System.out.println(“#################”); System.out.println(“&&&&&&&&&&&”); } } Which of the following will the output be?

    • A.

      ===============

    • B.

      ################# &&&&&&&&&

    • C.

      &&&&&&&&&&&

    • D.

      =============== ################# &&&&&&&&&&

    Correct Answer
    C. &&&&&&&&&&&
    Explanation
    The output will be "&&&&&&&&&". This is because the if statement checks if "a" is equal to 3, which is false. Therefore, the inner if statement is not executed and the else statement is skipped. As a result, only the last System.out.println statement is executed, which prints "&&&&&&&&&".

    Rate this question:

  • 25. 

    Consider the following program: class prob1{ int puzzel(int n){ int result; if (n==1) return 1; result = puzzel(n-1) * n; return result; } } class prob2{ public static void main(String args[]) { prob1 f = new prob1(); System.out.println(” puzzel of 6 is = ” + f.puzzel(6)); } } Which of the following will be the output of the above program?

    • A.

      6

    • B.

      120

    • C.

      30

    • D.

      720

    Correct Answer
    D. 720
    Explanation
    The program defines a class called prob1 with a method called puzzel. The puzzel method takes an integer n as input and recursively calculates the factorial of n. In the main method of the prob2 class, an instance of prob1 is created and the puzzel method is called with an input of 6. The output of the program will be 720, which is the factorial of 6.

    Rate this question:

  • 26. 

    What will be the output of the following program?
    1. public class Test {
    2. public static void main(String[] args) {
    3. int count = 1;
    4. while (count <= 15) {
    5. System.out.println(count % 2 == 1 ? "***" : "+++++");
    6. ++count;
    7. } // end while
    8. } // end main
    9. }

    • A.

      15 times ***

    • B.

      15 times +++++

    • C.

      8 times *** and 7 times +++++

    • D.

      Both will print only once

    Correct Answer
    C. 8 times *** and 7 times +++++
    Explanation
    The program uses a while loop to iterate from 1 to 15. Inside the loop, it checks if the current count is odd or even using the expression "count % 2 == 1". If the count is odd, it prints "***", otherwise it prints "+++++". Since there are 8 odd numbers and 7 even numbers between 1 and 15, the output will be 8 times "***" and 7 times "+++++".

    Rate this question:

  • 27. 

    What will be the output of the following program?
    1. public class Test2 {
    2. public static void main(String[] args) {
    3. StringBuffer s1 = new StringBuffer("Complete");
    4. s1.setCharAt(1,'i');
    5. s1.setCharAt(7,'d');
    6. System.out.println(s1);
    7. }
    8. }

    • A.

      Complete

    • B.

      Iomplede

    • C.

      Cimpletd

    • D.

      Coipletd

    Correct Answer
    C. Cimpletd
    Explanation
    The program creates a StringBuffer object with the initial value "Complete". The setCharAt() method is then used to replace the character at index 1 with 'i' and the character at index 7 with 'd'. Therefore, the output will be "Cimpletd".

    Rate this question:

  • 28. 

    Given,
    1. int values[ ] = {1,2,3,4,5,6,7,8,9,10};
    2. for(int i=0;i< Y; ++i)
    3. System.out.println(values[i]);
    Find the value of value[i]?

    • A.

      10

    • B.

      11

    • C.

      15

    • D.

      None of These

    Correct Answer
    D. None of These
  • 29. 

    Class Main { public static void main(String args[]) { try { throw 10; } catch(int e) { System.out.println("Got the Exception " + e); } } }  Find the output of the code.

    • A.

      Got the Exception 10

    • B.

      Got the Exception 0

    • C.

      Compiler error

    • D.

      Runtime error

    Correct Answer
    C. Compiler error
    Explanation
    The given code will result in a compiler error because the code is attempting to throw an integer value (10) instead of an exception object. The catch block is expecting an exception object to handle, not an integer value.

    Rate this question:

  • 30. 

    Predict the output of the following program. class Test { String str = "a"; void A() { try { str +="b"; B(); } catch (Exception e) { str += "c"; } } void B() throws Exception { try { str += "d"; C(); } catch(Exception e) { throw new Exception(); } finally { str += "e"; } str += "f"; } void C() throws Exception { throw new Exception(); } void display() { System.out.println(str); } public static void main(String[] args) { Test object = new Test(); object.A(); object.display(); } }

    • A.

      Abdef

    • B.

      Abdefc

    • C.

      Abedc

    • D.

      Abdec

    Correct Answer
    D. Abdec
    Explanation
    The program starts by creating an object of the Test class and calling the method A(). In method A(), it tries to concatenate "b" to the string "str" and then calls method B(). In method B(), it tries to concatenate "d" to the string "str" and then calls method C(). In method C(), it throws an exception. Since method C() is called within a try block in method B(), the catch block is executed. In the catch block, "c" is concatenated to the string "str". Finally, "e" is concatenated to the string "str" and then "f" is concatenated outside the try-catch block in method B(). Finally, the display() method is called and it prints the value of "str", which is "abdec".

    Rate this question:

Back to Top Back to top
Advertisement