adplus-dvertising

Welcome to the Classes and Methods MCQs Page

Dive deep into the fascinating world of Classes and Methods with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Classes and Methods, a crucial aspect of Java Programming. In this section, you will encounter a diverse range of MCQs that cover various aspects of Classes and Methods, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Java Programming.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Classes and Methods. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Java Programming.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Classes and Methods. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Classes and Methods MCQs | Page 14 of 30

Explore more Topics under Java Programming

Q131.
Which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?
public class MyOuter 
{
    public static class MyInner 
    {
        public static void foo() { }
    }
}
Discuss
Answer: (a).MyOuter.MyInner m = new MyOuter.MyInner();
Discuss
Answer: (c).It can extend exactly one class or implement exactly one interface.
Q133.

Which one create an anonymous inner class from within class Bar?
class Boo 
{
    Boo(String s) { }
    Boo() { }
}
class Bar extends Boo 
{
    Bar() { }
    Bar(String s) {super(s);}
    void zoo() 
    {
    // insert code here
    }
}
Discuss
Answer: (b).Boo f = new Bar() { };
Discuss
Answer: (b).It can be marked abstract.
Discuss
Answer: (b).It does not have access to nonstatic members of the enclosing class.
Discuss
Answer: (d).System.out.println(new Runnable() {public void run() { }});
Q137.
What will be the output of the program?

public class Foo 
{
    Foo() 
    {
        System.out.print("foo");
    }
    
class Bar
{
    Bar() 
    {
        System.out.print("bar");
    }
    public void go() 
    {
        System.out.print("hi");
    }
} /* class Bar ends */

    public static void main (String [] args) 
    {
        Foo f = new Foo();
        f.makeBar();
    }
    void makeBar() 
    {
        (new Bar() {}).go();
    }
}/* class Foo ends */
Discuss
Answer: (c).It prints "foobarhi"
Q138.
What will be the output of the program?

public class HorseTest 
{
    public static void main (String [] args) 
    {
        class Horse 
        {
            public String name; /* Line 7 */
            public Horse(String s) 
            {
                name = s;
            }
        } /* class Horse ends */
        
        Object obj = new Horse("Zippo"); /* Line 13 */
        Horse h = (Horse) obj; /* Line 14 */
        System.out.println(h.name);
    }
} /* class HorseTest ends */
Discuss
Answer: (b).It prints "Zippo".
Q139.
What will be the output of the program?

public class TestObj 
{
    public static void main (String [] args) 
    {
        Object o = new Object() /* Line 5 */
        {
            public boolean equals(Object obj) 
            {
                return true;
            } 
        }      /* Line 11 */
        
        System.out.println(o.equals("Fred"));
    }
}
Discuss
Answer: (d).Compilation fails
Q140.
What will be the output of the program?

public abstract class AbstractTest 
{
    public int getNum() 
    {
        return 45;
    }
    public abstract class Bar 
    {
        public int getNum() 
        {
            return 38;
        }
    }
    public static void main (String [] args) 
    {
        AbstractTest t = new AbstractTest() 
        {
            public int getNum() 
            {
                return 22;
            }
        };
        AbstractTest.Bar f = t.new Bar() 
        {
            public int getNum() 
            {
                return 57;
            }
        };
        
        System.out.println(f.getNum() + " " + t.getNum());
    }
}
Discuss
Answer: (a).57 22

Suggested Topics

Are you eager to expand your knowledge beyond Java Programming? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!