Menu
Topics Index
...
`

Interfaces1019


If you need explanation Read this topic

If you need Answer Take test on this topic

Interfaces1019
What will be the output of the following program?
class Company
{
    public static void main(String args[])
    {
        Software.NestedIF nif = new Project();
        
        if(nif.isNotNegative(10))
            System.out.println("10 is not negative");

        if(nif.isNotNegative(-12))
            System.out.println("This won't be displayed");
    }
}

class Software
{
    public interface NestedIF
    {
        boolean isNotNegative(int x);
    }
}

class Project implements Software.NestedIF
{
    public boolean isNotNegative(int x)
    {
        return x < 0 ? false : true;
    }
}

A. 10 is not negative
This won't be displayed
B. 10 is not negative
C. This won't be displayed
D. Compilation Error
E. Runtime Error
Topic: Java Interface

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 b .this program have 3 class and 1 interface..here interface is used lik method. thn reference for project class object created.if condition called the isNotNegative func and 10 is passed as an argument. that method returns true bcz 10< 0 fails so returns true.thn sop get printed.2 nd if called the isnotnegative() method  with -12.method returns false -12<0

Posted by Maheshwari Natarajan    2014-10-31 06:15:10


Ans is B...

Posted by Raju VarshnEy    2014-10-31 06:42:53


Ans is B..here we have the concept of inner interface..an interface can be inside an interface ot inside an class..we cant access these interfaces directly we must access it with the class name or interface name..

here we have interface named with NestedIf inside the class Software..

class Project implementing these interface...as i said we cant access these interface directly we must use the class name..here it is implementing using the class name and Project class is overriding the isNotNegative()..

than we create reference for the and Object for the Project class..

PARENT CLASS REFERENCE CAN REFER CHILD CLASS OBJECT...

in if condtion we are calling the method here we are passing 10 than it is >0 so we are returning true than we are printing 10 is not negative next we are passing -12 than <0 so returning false so Sop() wont exeute...

Posted by Uday Kumar    2014-10-31 16:49:29


B

Posted by Shubham Bansal    2014-10-31 17:01:37


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-03 01:05:25


© meritcampus 2019

All Rights Reserved.

Open In App