Attempting To Compile And Run The Java Program

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 Venkatasanthosh
V
Venkatasanthosh
Community Contributor
Quizzes Created: 1 | Total Attempts: 580
| Attempts: 580 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1.
class MWC200 {
  public static void main (String[] args) {
    String s1 = "ABC";
    StringBuffer s2 = new StringBuffer(s1);
    System.out.print(s2.equals(s1) + "," + s1.equals(s2));
}}
What is the result of attempting to compile and run the program?

Explanation

StringBuffer.equals does not override the Object.equals method; so StringBuffer.equals just compares reference values. Since the reference variables s1 and s2 refer to different objects, the equals method of the StringBuffer instance s2 returns the value false. The String.equals method does override the Object.equals, method. The String.equals method returns false anytime the argument is not an instance of type String. The method invocation expression s1.equals(s2) produces the value false, because the argument is an instance of type StringBuffer

Submit
Please wait...
About This Quiz
Attempting To Compile And Run The Java Program - Quiz

This Java programming quiz assesses understanding of exception handling, interface methods, and static context manipulations. It tests key skills like error handling, interface design, and string manipulation in... see moreJava, providing insights essential for any Java developer. see less

2.
class MWC106 {
  static void m1(String s) {
    s = s.trim(); s = s.concat("D");
  }
  public static void main(String[] s) {
    String s1 = "A", s2 = " B ", s3 = "C";
    m1(s2);
    System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?

Explanation

The String instance referenced by s2 is passed to the m1 method by passing the value of the reference. The reference value used in method m1 is a local copy of the reference. If the local copy used in method m1 is changed, then the original reference variable in the main method remains unchanged.

Submit
3.
What is the result of compiling and running this program?

      class Mammal{
         void eat(Mammal m){
         System.out.println("Mammal eats food");
         }
      }
      class Cattle extends Mammal{
         void eat(Cattle c){
         System.out.println("Cattle eats hay");
         }
      }

      class Horse extends Cattle{
          void eat(Horse h){
          System.out.println("Horse eats hay");
          }
      }
      public class Test{
        public static void main(String[] args){
          Mammal h = new Horse();
          Cattle c = new Horse();
          c.eat(h);
        }
      }

Explanation

The method that will be called is the one
from class Mammal. The reasons are quite obvious.

Submit
4.
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
  void m1() throws ColorException {throw new WhiteException();}
  void m2() throws WhiteException {}
  public static void main (String[] args) {
    White white = new White();
    int a,b,d,f; a = b = d = f = 0;
    try {white.m1(); a++;} catch (ColorException e) {b++;}
    try {white.m2(); d++;} catch (WhiteException e) {f++;}
    System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?

Explanation

The first try block contains two statements. The first invokes method m1, and the subsequent statement contains a post increment expression with the variable, a, as the operand. Method m1 throws a WhiteException exception, so variable a is not incremented as control passes to the catch block where b is incremented. The throws clause of m1 declares a ColorException, so the body may throw a ColorException or any subclass of ColorException. The second try block also contains two statements. The first invokes method m2, and the subsequent statement contains a post increment expression with the variable, d, as the operand. Method m2 does not throw an exception, so d is incremented, and the try block completes normally. Although the throws clause of m2 declares a WhiteException, there is no requirement to throw any exception.

Submit
5.
import java.util.*;
class GFC112 {
  public static void main (String[] args) {
    Object m = new LinkedHashSet();
    System.out.print((m instanceof Collection)+",");
    System.out.print((m instanceof Set)+",");
    System.out.print(m instanceof List);
}}
What is the result of attempting to compile and run the program?

Explanation

LinkedHashSet does not implement the List interface. LinkedHashSet extends HashSet and implements Collection, Set, Cloneable and Serializable

Submit
6. Which of the following are not methods of the java.lang.String class?

Explanation

The StringBuffer class has methods named append, delete and insert, but the String class does not. A typical trick question will attempt to invoke StringBuffer methods on a String instance

Submit
7.
class MWC117 {
  public static void main (String[] args) {
    System.out.print(String.valueOf(1) + String.valueOf(2));
    String s1 = "S1";
    String s2 = s1.toString();
    System.out.print("," + (s1==s2));
}}
What is the result of attempting to compile and run the program?

Explanation

The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object, char[], or any primitive type. The toString method returns a reference to the existing String instance. It does not create a new instance of the String.

Submit
8. Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing interface?

Explanation

All interfaces are implicitly abstract. The explicit application of the abstract modifier to an interface declaration is redundant and is strongly discouraged. The declaration of an interface within the body of an enclosing class or interface is called a member type declaration. Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public. Use of the access modifiers, private or protected, is contradictory and results in a compile-time error. In contrast, the modifiers, private and protected, are applicable to a member type declaration appearing within the body of a directly enclosing class. The modifier, final, is never applicable to an interface. The keyword, implements, is not a modifier.

Submit
9.
interface A {
  int a = 1;                        // 1
  public int b = 2;                 // 2
  public static int c = 3;          // 3
  public static final int d = 4;    // 4
}
Which field declaration results in a compile-time error?

Explanation

All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface.

Submit
10.
interface A {
  void m1();            // 1
  public void m2();     // 2
  protected void m3();  // 3
  private void m4();    // 4
}
Compile-time errors are generated at which lines?

Explanation

Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

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

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 01, 2012
    Quiz Created by
    Venkatasanthosh
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Class MWC200 {...
Class MWC106 {...
What is the result of compiling and running this program?...
Class ColorException extends Exception {}...
Import java.util.*;...
Which of the following are not methods of...
Class MWC117 {...
Which of the following are modifiers that can be applied to an...
Interface A {...
Interface A {...
Alert!

Advertisement