Topics Index

Maggi and Noodles

If you need explanation Read this topic

If you need Answer Take test on this topic

Maggi and Noodles
What will be the output of the following program?
class FoodItems
    public static void main(String s[])
        Noodles eat = new Noodles();

class Maggi
    private int spoon = 3;
    protected int sticks = 4;

class Noodles extends Maggi
    private int powder = 1;
    protected int fire = 2;

    void print()
        System.out.println(powder + " " + fire + " " + sticks + " " + spoon);

A. 1 2 4 3
B. 1 2 4
C. Compilation Error
D. Runtime Error
Topic: Access Modifiers In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is comple time error ...maggi clss havng private access specifiers...

Posted by Ashok Reddi    2014-02-19 06:04:52

ans :c it is not protected but private member spoon cannot be accessed directly by its sub class

Posted by Karthik Narahari    2014-02-19 07:13:51

Ans: c ..because In Maggi Class -spoon variable is a private .so the scope is not accessible in other class. even though it is subclass........... so we get compilation the remaining all is perfectly work. and then prints all variables..with out Ans: C

Posted by Sapparapu Pradeep Kumar    2014-02-19 07:41:44

Answer is C. This program results in a compilation error. The reason for that is private methods and fields can only be accessed within the same class to which they belong. They are not inherited by sub classes and hence are not visible within sub classes. Same is the case with "spoon" here, which is a private member of "Maggi" class, which the "Noodles" class is trying to access through SOP statement in the print method!

Posted by Shaileshwar Sharma    2014-02-19 08:46:33

Answer is c...Atlest one public class is required in the main file...

Posted by Sneha Rapool    2014-02-19 15:17:41

Congratulations Sapparapu Pradeep Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-20 04:22:20

© meritcampus 2019

All Rights Reserved.

Open In App