This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Member Functions”.
1. What is the output of the following Java code?
class Function { void Print() { System.out.println("Hello World!"); } public static void main(String[] args) { Function obj=new Function(); obj.Print(); } }
a) Hello World
b) Compilation error
c) Null
d) Hello World!
View Answer
Explanation: A function is a block of code which only runs when it is called. If a function is present and called in a class, it is called the member function of that particular class. In this case, Print() is a member function which is called in the main method using an object ‘obj’.
Output:
$ javac Function.java $ java Function Hello World!
2. What is the output of the following Java code?
class TestCase { public static void main(String[] args) { Function(); } private static void Function() { System.out.println("Print successful!"); } }
a) Print successful!
b) Compilation error
c) Null
d) Garbage Value
View Answer
Explanation: Methods are declared within a class and they are used to perform certain actions defined in it. In this case, even if the member method Function() is of protection level private, it is accessible in the same class in which it is present. Hence, the method is executed successfully.
Output:
$ javac TestCase.java $ java TestCase Print successful!
3. What is the output of the following Java code?
class MinMax { public static void main(String[] args) { int a = 12; int b = 8; int c = Calc(a, b); System.out.println(c); } public static int Calc(int n1, int n2) { int min; if (n1 > n2) { min = n2; } else { min = n1; } return min; } }
a) 12
b) 8
c) Garbage Value
d) Compilation error
View Answer
Explanation: A Java method is a collection of statements that are grouped together to perform an operation as mentioned by the user. In the above program, a static member function Calc() is used to find the minimum of two numbers. The function is called successfully in the main() method.
Output:
$ javac MinMax.java $ java MinMax 8
4. What is the output of the following Java code?
class SubtractionofNum { public static void main(String[] args) { Prog1 obj=new Prog1(); obj.callSub(); } public void callSub() { int theSum = sub(7, 3); System.out.print(theSum); } public int sub(int value1, int value2) { return (value1 - value2); } }
a) 4
b) Null
c) Compilation error
d) -4
View Answer
Explanation: A Java method is a collection of statements that are grouped together to perform an operation as mentioned by the user. In the above program, a member function sub() is called within another method callSub(). Since both are present in the same class, it is possible to perform this operation.
Output:
$ javac SubtractionofNum.java $ java SubtractionofNum 4
5. What is the output of the following Java code?
class Depreciation { public static void main(String arg[]) { int amount, dep, year; System.out.println(Depreciation.Calc(10000, 10, 1)); } static int Calc(int amount, int dep, int year ) { for(int i=0;i<year;i++) { amount=((100-dep)*amount)/100; } return amount; } }
a) 9000
b) 8000
c) 10000
d) 1000
View Answer
Explanation: A method is a block of code which only runs when it is called. In the above program, the value is passed in the main() method to the Calc() function which returns a value.
Output:
9000
6. What is the output of the following Java code?
class Triangle { public static void main(String args[]) { Triangle obj=new Triangle(); obj.Area(10, 15.0); } void Area(int b,int h) { double area=(b*h)/2; System.out.println(area); } }
a) 75
b) 150
c) Garbage Value
d) Compilation error
View Answer
Explanation: A function is a set of statements that take inputs, do some specific computation and produces output. In this case, method Area() is a member function of class Triangle. Since the value of ‘h’ is given as a double in the function call, an error is thrown.
Output:
$ javac Triangle.java
$ java Triangle
Uncompilable source code
7. What is the output of the following Java code?
class TriangleArea extends Square { void Area(int b, int h) { double area=(b*h)/2; System.out.println(area); } public static void main(String args[]) { TriangleArea obj=new TriangleArea(); obj.Area(5); } } class Square { void Area(int l) { System.out.println(l*l); } }
a) 25
b) 0
c) 12
d) Compilation error
View Answer
Explanation: In the above program, the concept of function overloading is used. As there are two methods of same name ‘Area’, the compiler differentiates between them using the number and the type of arguements that are passed to the function. As there is only one parameter in the function call, the compiler executes the Area() method inside the Square class.
Output:
$ javac TriangleArea.java $ java TriangleArea 25
8. What is the output of the following Java code?
class Perimeter { static double area(double l, double b) { double a=2*(l+b); return a; } public static void main(String args[]) { System.out.println(Perimeter.area(10, 20)); } }
a) 60
b) 30
c) 200
d) Compilation error
View Answer
Explanation: A method is a block of code which only runs when it is called. A member function is a part of the current class in which the execution is taking place. In this case, area() is a static method and hence the function can be called without an object.
Output:
60
9. What is the output of the following Java code?
class Conversion { double celsius(double f) { return (f-32)*5/9; } public static void main(String args[]) { Conversion obj=new Conversion(); double result=obj.celsius(100); System.out.println(result); } }
a) 212
b) 37.777
c) 100
d) Compilation error
View Answer
Explanation: A Java method is a collection of statements that are grouped together to perform an operation. The above program is used to convert Fahrenheit to Celsius. As celsius() method is a part of class Conversion, it is called in the main() method using the object created.
Output:
$ javac Conversion.java $ java Conversion 37.777
10. What is the output of the following Java code?
class HCF { void Prime() { int n1=10; int n2=6; int temp; while (n2 != 0) { temp = n2; n2 = n1% n2; n1 = temp; } System.out.println(n1); } public static void main(String args[]) { HCF obj=new HCF(); obj.Prime(); } }
a) 3
b) 2
c) 6
d) 10
View Answer
Explanation: Methods are defined as blocks of code which is used to maintain reusability and perform various operations. In this case, prime() method is called in the main() method using the object of class type HCF and hence execution takes place without any error and the highest common factor is printed.
Output:
$ javac HCF.java $ java HCF 2
11. What is the output of the following Java code?
class Time { int sec; void seconds(int m) { sec=m*60; System.out.println(sec); } public static void main(String[] args) { int min=60; Time obj=new Time(); obj.seconds(min); } }
a) 1
b) 3600
c) 360
d) Compilation error
View Answer
Explanation: A function is a block of code that performs a specific task. Method seconds() is used to convert minutes into seconds. It is called in the main() method using the object ‘obj’ of class Time.
Output:
$ javac Time.java $ java Time 3600
12. What is the output of the following Java code?
class Loop { void loop() { for(int i = 0;i!=0;i++); { System.out.println("Hello"); break; } System.out.println("Done"); } public static void main(String[] args) { Method obj=new Method(); obj.loop(); } }
a)
Hello Done
b)
Hello Hello Done
c) Done
d) Compilation error
View Answer
Explanation: Methods are declared within a class and they are used to perform certain actions defined by the user. The object ‘obj’ is created but inside the loop() function, the for loop has a semi colon next to it which leads to an error as the value of ‘i’ would not be valid inside the braces.
Output:
$ javac Loop.java
$ java Loop
Uncompilable source code
13. What is the output of the following Java code?
class Sum { static int sum(int num) { int sum=0; while(num!=0) { sum=sum + (num%10); num=num/10; } return sum; } public static void main(String arg[]) { System.out.println(sum(195)); } }
a) 15
b) 14
c) Compilation error
d) 0
View Answer
Explanation: A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. In the above program, the static function sum() is used to find the sum of digits in a given number. Since the method is static, there is no need to create an object.
Output:
$ javac Sum.java $ java Sum 15
14. What is the output of the following Java code?
class Circle { public static void main(String args[]) { System.out.println(Circle.circum(7)); } public static double circum(double r) { double a=(22*2*r)/7; return a; } }
a) 44
b) 44.0
c) 484
d) Compilation error
View Answer
Explanation: A Java method is a collection of statements that are grouped together to perform an operation as mentioned by the user. The above code consists of a static function circum() which returns a double value. Since it is a double value, there should be a value after the decimal place.
Output:
$ javac Circle.java $ java Circle 44.0
15. What is the output of the following Java code?
class LeapYear { static int flag=0; public static void main(String arg[]) { System.out.print(leap(2004)); System.out.print(leap(2000)); System.out.print(leap(1900)); } static int leap(int year) { if(year!=0) { if(year%400==0) { flag=1; } else if(year%100==0) { flag=0; } else if(year%4==0) { flag=1; } else { flag=0; } } return flag; } }
a) 110
b) 111
c) 001
d) Compilation error
View Answer
Explanation: A method is basically a set of code which is referred to by name and can be called or invoked at any point in a program. In this case, a static method leap() returns an integer flag to check whether the given year is leap or not. As ‘flag’ is a static variable, it can be accessed inside both the main() and leap() methods and execution will be done successfully. It displays 1 for a leap year and 0 for a non-leap year.
Output:
$ javac LeapYear.java $ java LeapYear 110
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]