English

What does the following class output? class output { public void work() { int totalCost = 6; int items = 12; System.out.println("cost per item: "+ totalCost/items); }}

Advertisements

Question

What does the following class output?

class output
{
public void work()
{
int totalCost = 6;
int items = 12;
System.out.println("cost per item: "+ totalCost/items);
}}

Options

  • cost per item: .5

  • cost per item: 0

  • cost per item: 1

  • cost per item: +6/12

MCQ
Advertisements

Solution

cost per item: 0

Explanation:

Both totalCost and items are integers. In Java, integer division (6 / 12) gives 0 because the fractional part is discarded.

shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 133]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 50. | Page 133
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×