KEMBAR78
User Defined Method Mcqs | PDF | Method (Computer Programming) | Parameter (Computer Programming)
0% found this document useful (0 votes)
144 views30 pages

User Defined Method Mcqs

The document consists of a series of questions and answers related to user-defined methods in Java. It covers topics such as method declaration, return types, method overloading, access modifiers, and method parameters. Each question is followed by the correct answer, providing a comprehensive overview of Java methods.

Uploaded by

vpinku35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views30 pages

User Defined Method Mcqs

The document consists of a series of questions and answers related to user-defined methods in Java. It covers topics such as method declaration, return types, method overloading, access modifiers, and method parameters. Each question is followed by the correct answer, providing a comprehensive overview of Java methods.

Uploaded by

vpinku35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

1. What is a user-defined method in Java?

a) A method provided by the Java library


b) A method created by the programmer
c) A method automatically generated by the compiler
d) A method used only in the main class
Answer: b

2. Which keyword is used to define a method that does not return any value?
a) int
b) void
c) return
d) static
Answer: b

3. How is a user-defined method declared in Java?


a) returnType methodName(parameters) { }
b) methodName returnType(parameters) { }
c) parameters methodName(returnType) { }
d) void methodName returnType { }
Answer: a

4. What is the purpose of the 'return' statement in a method?


a) To define the method name
b) To send a value back to the calling method
c) To declare a variable
d) To end the program
Answer: b

5. Which of the following is a valid method declaration?


a) int add(int a, int b) { return a + b; }
b) add int(a, b) { return a + b; }
c) int add(a, b) { a + b; }
d) void add(int a, b) { return a + b; }
Answer: a

6. Can a user-defined method be called without creating an object of the class?


a) Yes, if it is static
b) No, always requires an object
c) Yes, if it is void
d) No, methods cannot be called
Answer: a

7. What is method overloading?


a) Defining multiple methods with the same name but different parameters
b) Defining a method with no parameters
c) Calling a method multiple times
d) Changing the return type of a method
Answer: a

8. Which access modifier makes a method accessible only within the same class?
a) public
b) private
c) protected
d) default
Answer: b

9. What is the term for the values passed to a method when it is called?
a) Parameters
b) Arguments
c) Return values
d) Variables
Answer: b

10. What is the output of the following code if called correctly?


void printHello() { System.out.println("Hello"); }
a) Hello
b) Error
c) No output
d) printHello
Answer: a

11. How many return statements can a method have?


a) Only one
b) At least one
c) Multiple, but only one is executed
d) None
Answer: c

12. Which keyword is used to make a method accessible from any class?
a) private
b) public
c) protected
d) static
Answer: b

13. What is the correct syntax to call a method named 'calculate' with two integer parameters?
a) calculate(5, 10);
b) calculate[5, 10];
c) calculate{5, 10};
d) calculate(5; 10);
Answer: a

14. What happens if a method is declared without a return type?


a) It becomes a void method
b) It causes a compilation error
c) It returns an integer by default
d) It runs without any issue
Answer: b

15. Which of the following is NOT a valid method name in Java?


a) calculateSum
b) 2ndMethod
c) findMax
d) print_Result
Answer: b

16. What does the 'static' keyword indicate in a method declaration?


a) The method cannot return a value
b) The method belongs to the class, not an object
c) The method is private
d) The method must have parameters
Answer: b

17. What is the output of the following code?


int square(int x) { return x * x; }
// Called as: System.out.println(square(4));
a) 16
b) 4
c) 8
d) Error
Answer: a

18. Can a method have the same name as the class?


a) Yes, if it is a constructor
b) No, it causes an error
c) Yes, if it is static
d) No, unless it is void
Answer: a

19. What is the purpose of parameters in a method?


a) To store the method's return value
b) To provide input data to the method
c) To define the method's access level
d) To call another method
Answer: b

20. Which of the following methods can be overloaded?


a) Methods with different return types only
b) Methods with the same name but different parameter lists
c) Methods with the same parameters and return type
d) Methods with different access modifiers only
Answer: b

21. What is the output of the following code?


void display(int x) { System.out.println(x); }
// Called as: display(10);
a) 10
b) Error
c) No output
d) display
Answer: a

22. Which modifier allows a method to be accessed within the same package?
a) private
b) public
c) protected
d) default
Answer: d
23. What is the result of calling a method with incorrect argument types?
a) It runs normally
b) It causes a compilation error
c) It returns null
d) It skips the method
Answer: b

24. Can a method be defined inside another method in Java?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: b

25. What is the output of the following code?


int add(int a, int b) { return a + b; }
// Called as: System.out.println(add(3, 5));
a) 8
b) 15
c) 3
d) Error
Answer: a

26. Which keyword is used to invoke a method from within the same class?
a) this
b) super
c) call
d) invoke
Answer: a

27. What is a method's signature in Java?


a) Method name and its parameters
b) Method name and return type
c) Method name, parameters, and return type
d) Method name and access modifier
Answer: a

28. What happens if a method is called with fewer arguments than its parameters?
a) It uses default values
b) It causes a compilation error
c) It ignores extra parameters
d) It runs without arguments
Answer: b

29. Which of the following is a valid method call?


a) int sum(5, 10);
b) sum(5, 10);
c) sum[5, 10];
d) sum{5, 10};
Answer: b

30. What is the output of the following code?


void printName(String name) { System.out.println("Hello, " + name); }
// Called as: printName("Alice");
a) Hello, Alice
b) Error
c) Alice
d) No output
Answer: a

31. Can a method return multiple values in Java?


a) Yes, directly
b) No, but it can return an array or object
c) Yes, using multiple return statements
d) No, it always returns one value
Answer: b

32. What is the purpose of the 'public' access modifier in a method?


a) Restricts access to the class
b) Allows access from any class
c) Allows access within the package
d) Makes the method static
Answer: b

33. What is the output of the following code?


int max(int a, int b) { return a > b ? a : b; }
// Called as: System.out.println(max(7, 3));
a) 7
b) 3
c) 10
d) Error
Answer: a

34. Which of the following is a valid method declaration with no parameters?


a) void method() { }
b) method void() { }
c) void method { }
d) method() { }
Answer: a

35. What happens if a method does not include a return statement but is declared with a
return type?
a) It returns null
b) It causes a compilation error
c) It returns zero
d) It runs normally
Answer: b

36. What is the output of the following code?


static void sayHi() { System.out.println("Hi"); }
// Called as: sayHi();
a) Hi
b) Error
c) No output
d) sayHi
Answer: a

37. Which of the following is true about method overloading?


a) Methods must have different return types
b) Methods must have the same number of parameters
c) Methods must have different parameter lists
d) Methods cannot be static
Answer: c

38. What is the purpose of the 'protected' access modifier?


a) Allows access only within the same class
b) Allows access within the same package and subclasses
c) Allows access from any class
d) Makes the method static
Answer: b
39. What is the output of the following code?
int multiply(int x, int y) { return x * y; }
// Called as: System.out.println(multiply(2, 3));
a) 6
b) 5
c) 8
d) Error
Answer: a

40. Can a method be declared with the 'final' keyword?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

41. What does the 'final' keyword do when applied to a method?


a) Prevents method overriding
b) Makes the method static
c) Prevents method overloading
d) Makes the method private
Answer: a

42. What is the output of the following code?


void showNumber(int num) { System.out.println(num); }
// Called as: showNumber(100);
a) 100
b) Error
c) No output
d) showNumber
Answer: a

43. Which of the following is a correct method declaration with a String return type?
a) String getName() { return "John"; }
b) getName String() { return "John"; }
c) String getName { return "John"; }
d) String() getName { return "John"; }
Answer: a
44. What happens if a method is called with more arguments than its parameters?
a) It ignores extra arguments
b) It causes a compilation error
c) It uses default values
d) It runs normally
Answer: b

45. What is the output of the following code?


int cube(int n) { return n * n * n; }
// Called as: System.out.println(cube(2));
a) 8
b) 4
c) 6
d) Error
Answer: a

46. Can a method have no parameters?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

47. What is the output of the following code?


void printMessage() { System.out.println("Welcome"); }
// Called as: printMessage();
a) Welcome
b) Error
c) No output
d) printMessage
Answer: a

48. Which of the following is a valid static method declaration?


a) static void method() { }
b) void static method() { }
c) static method void() { }
d) method static() { }
Answer: a

49. What is the purpose of method parameters?


a) To return multiple values
b) To accept input for the method
c) To define the method's return type
d) To make the method static
Answer: b

50. What is the output of the following code?


int divide(int a, int b) { return a / b; }
// Called as: System.out.println(divide(10, 2));
a) 5
b) 20
c) 2
d) Error
Answer: a

51. Can a method be called recursively in Java?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

52. What is the output of the following code?


void greet(String name) { System.out.println("Hi, " + name); }
// Called as: greet("Bob");
a) Hi, Bob
b) Error
c) Bob
d) No output
Answer: a

53. Which access modifier allows a method to be accessed only by subclasses?


a) public
b) private
c) protected
d) default
Answer: c

54. What is the output of the following code?


int sum(int x, int y, int z) { return x + y + z; }
// Called as: System.out.println(sum(1, 2, 3));
a) 6
b) 5
c) 3
d) Error
Answer: a

55. What happens if a method is declared with a return type but returns nothing?
a) It returns null
b) It causes a compilation error
c) It returns zero
d) It runs normally
Answer: b

56. Which of the following is a valid method call for a method with no parameters?
a) method();
b) method[];
c) method{};
d) method();
Answer: a

57. What is the output of the following code?


static int addNumbers(int a, int b) { return a + b; }
// Called as: System.out.println(addNumbers(4, 6));
a) 10
b) 24
c) 4
d) Error
Answer: a

58. Can a method have the same name as a variable in the same class?
a) Yes
b) No
c) Only if the variable is static
d) Only if the method is void
Answer: a

59. What is the purpose of the 'this' keyword in a method?


a) To refer to the current object
b) To call another method
c) To return a value
d) To make the method static
Answer: a

60. What is the output of the following code?


void displayText(String text) { System.out.println(text); }
// Called as: displayText("Java");
a) Java
b) Error
c) No output
d) displayText
Answer: a

61. Which of the following is a valid method declaration with a double return type?
a) double calculate() { return 5.5; }
b) calculate double() { return 5.5; }
c) double calculate { return 5.5; }
d) double() calculate { return 5.5; }
Answer: a

62. What happens if a method tries to return a value of the wrong type?
a) It causes a compilation error
b) It converts the value automatically
c) It returns null
d) It runs normally
Answer: a

63. What is the output of the following code?


int subtract(int a, int b) { return a - b; }
// Called as: System.out.println(subtract(10, 4));
a) 6
b) 14
c) 4
d) Error
Answer: a

64. Can a method be declared inside a class but outside any method?
a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

65. What is the output of the following code?


void showValue(int x) { System.out.println(x * 2); }
// Called as: showValue(5);
a) 10
b) 5
c) Error
d) No output
Answer: a

66. Which of the following is true about a void method?


a) It must return a value
b) It cannot return a value
c) It must have parameters
d) It cannot be called
Answer: b

67. What is the output of the following code?


int getSquare(int n) { return n * n; }
// Called as: System.out.println(getSquare(5));
a) 25
b) 10
c) 5
d) Error
Answer: a

68. What is the purpose of method overloading in Java?


a) To reuse the same method name with different functionality
b) To make methods static
c) To prevent method calls
d) To change the access modifier
Answer: a

69. Which of the following is a valid method declaration with a boolean return type?
a) boolean isEven(int n) { return n % 2 == 0; }
b) isEven boolean(int n) { return n % 2 == 0; }
c) boolean isEven { return n % 2 == 0; }
d) boolean isEven(int n) { n % 2 == 0; }
Answer: a

70. What is the output of the following code?


void printDouble(int x) { System.out.println(x * 2); }
// Called as: printDouble(3);
a) 6
b) 3
c) Error
d) No output
Answer: a

71. Can a method be called from another method in the same class?
a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

72. What is the output of the following code?


int min(int a, int b) { return a < b ? a : b; }
// Called as: System.out.println(min(5, 8));
a) 5
b) 8
c) 13
d) Error
Answer: a

73. Which access modifier restricts a method to the same package unless inherited?
a) public
b) private
c) protected
d) default
Answer: d

74. What is the output of the following code?


void printSum(int a, int b) { System.out.println(a + b); }
// Called as: printSum(2, 3);
a) 5
b) Error
c) No output
d) printSum
Answer: a

75. Can a method have multiple parameters of different types?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

76. What is the output of the following code?


int product(int x, int y) { return x * y; }
// Called as: System.out.println(product(4, 5));
a) 20
b) 9
c) 4
d) Error
Answer: a

77. What happens if a method is declared with 'private' access?


a) It can be accessed from any class
b) It can only be accessed within the same class
c) It can be accessed within the same package
d) It becomes static
Answer: b

78. What is the output of the following code?


void displayChar(char c) { System.out.println(c); }
// Called as: displayChar('A');
a) A
b) Error
c) No output
d) displayChar
Answer: a

79. Which of the following is a valid method declaration with no return value?
a) void method() { }
b) method void() { }
c) void method { }
d) method() { }
Answer: a

80. What is the output of the following code?


int getDouble(int n) { return n * 2; }
// Called as: System.out.println(getDouble(6));
a) 12
b) 6
c) 3
d) Error
Answer: a

81. Can a method be overloaded with a different return type but the same parameters?
a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: b

82. What is the output of the following code?


void printString(String s) { System.out.println(s); }
// Called as: printString("Test");
a) Test
b) Error
c) No output
d) printString
Answer: a

83. Which keyword is used to call a method from a superclass?


a) this
b) super
c) call
d) invoke
Answer: b

84. What is the output of the following code?


int addThree(int a, int b, int c) { return a + b + c; }
// Called as: System.out.println(addThree(1, 2, 3));
a) 6
b) 5
c) 3
d) Error
Answer: a

85. What happens if a method is called before it is defined?


a) It causes a compilation error
b) It runs normally
c) It returns null
d) It skips the method
Answer: b

86. What is the output of the following code?


void showSquare(int n) { System.out.println(n * n); }
// Called as: showSquare(4);
a) 16
b) 4
c) Error
d) No output
Answer: a

87. Which of the following is a valid method name?


a) 1method
b) method_1
c) method-1
d) method@1
Answer: b

88. What is the output of the following code?


int divideNumbers(int a, int b) { return a / b; }
// Called as: System.out.println(divideNumbers(8, 2));
a) 4
b) 16
c) 2
d) Error
Answer: a

89. Can a method have the same name as another method in the same class?
a) Yes, if parameters are different
b) No
c) Only if it is static
d) Only if it is void
Answer: a

90. What is the output of the following code?


void printNumber(int x) { System.out.println(x); }
// Called as: printNumber(50);
a) 50
b) Error
c) No output
d) printNumber
Answer: a

91. What is the purpose of the 'static' keyword in a method?


a) To make the method return a value
b) To allow the method to be called without an object
c) To restrict access to the method
d) To make the method private
Answer: b

92. What is the output of the following code?


int getSum(int a, int b) { return a + b; }
// Called as: System.out.println(getSum(7, 8));
a) 15
b) 56
c) 7
d) Error
Answer: a

93. Which of the following is a valid method declaration with a char return type?
a) char getChar() { return 'A'; }
b) getChar char() { return 'A'; }
c) char getChar { return 'A'; }
d) char() getChar { return 'A'; }
Answer: a

94. What happens if a method is called with null arguments for primitive parameters?
a) It runs normally
b) It causes a compilation error
c) It causes a runtime error
d) It returns null
Answer: b
95. What is the output of the following code?
void printTriple(int x) { System.out.println(x * 3); }
// Called as: printTriple(5);
a) 15
b) 5
c) Error
d) No output
Answer: a

96. Can a method be declared with the 'abstract' keyword?


a) Yes, in an abstract class
b) No
c) Only if it is static
d) Only if it is void
Answer: a

97. What is the output of the following code?


int getCube(int n) { return n * n * n; }
// Called as: System.out.println(getCube(3));
a) 27
b) 9
c) 3
d) Error
Answer: a

98. Which of the following is true about method parameters?


a) They must always be primitive types
b) They can be primitive or reference types
c) They cannot be used in calculations
d) They must be static
Answer: b

99. What is the output of the following code?


void displayMessage() { System.out.println("Hello World"); }
// Called as: displayMessage();
a) Hello World
b) Error
c) No output
d) displayMessage
Answer: a

100. Can a method be overloaded with the same parameter types but different return types?
a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: b

101. What is the output of the following code?


int multiplyNumbers(int x, int y) { return x * y; }
// Called as: System.out.println(multiplyNumbers(3, 4));
a) 12
b) 7
c) 3
d) Error
Answer: a

102. What is the purpose of the 'return' keyword in a void method?


a) To return a value
b) To end the method execution
c) To call another method
d) To make the method static
Answer: b

103. What is the output of the following code?


void printValue(int x) { System.out.println(x + 1); }
// Called as: printValue(9);
a) 10
b) 9
c) Error
d) No output
Answer: a

104. Which access modifier allows a method to be accessed from subclasses in different
packages?
a) public
b) private
c) protected
d) default
Answer: c

105. What is the output of the following code?


int getMax(int a, int b) { return a > b ? a : b; }
// Called as: System.out.println(getMax(10, 5));
a) 10
b) 5
c) 15
d) Error
Answer: a

106. Can a method be declared with no access modifier?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

107. What is the output of the following code?


void showText(String s) { System.out.println(s); }
// Called as: showText("Coding");
a) Coding
b) Error
c) No output
d) showText
Answer: a

108. What is the default access modifier for a method if none is specified?
a) public
b) private
c) protected
d) default
Answer: d

109. What is the output of the following code?


int addNumbers(int a, int b, int c) { return a + b + c; }
// Called as: System.out.println(addNumbers(2, 3, 4));
a) 9
b) 6
c) 3
d) Error
Answer: a

110. Can a method return an object in Java?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

111. What is the output of the following code?


void printSquare(int n) { System.out.println(n * n); }
// Called as: printSquare(6);
a) 36
b) 6
c) Error
d) No output
Answer: a

112. Which of the following is a valid method declaration with an int array return type?
a) int[] getArray() { return new int[]{1, 2, 3}; }
b) getArray int[]() { return new int[]{1, 2, 3}; }
c) int[] getArray { return new int[]{1, 2, 3}; }
d) int[]() getArray { return new int[]{1, 2, 3}; }
Answer: a

113. What is the output of the following code?


int subtractNumbers(int a, int b) { return a - b; }
// Called as: System.out.println(subtractNumbers(8, 3));
a) 5
b) 11
c) 3
d) Error
Answer: a

114. Can a method be declared with the 'synchronized' keyword?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

115. What is the output of the following code?


void printGreeting(String name) { System.out.println("Hello, " + name); }
// Called as: printGreeting("Eve");
a) Hello, Eve
b) Error
c) Eve
d) No output
Answer: a

116. What is the purpose of the 'synchronized' keyword in a method?


a) To make the method static
b) To prevent multiple threads from executing it simultaneously
c) To make the method private
d) To allow multiple return types
Answer: b

117. What is the output of the following code?


int getProduct(int a, int b) { return a * b; }
// Called as: System.out.println(getProduct(5, 6));
a) 30
b) 11
c) 5
d) Error
Answer: a

118. Can a method have a variable number of arguments?


a) Yes, using varargs
b) No
c) Only if it is static
d) Only if it is void
Answer: a

119. What is the syntax for a method with variable arguments?


a) void method(int... numbers) { }
b) void method(int[] numbers) { }
c) void method(int numbers...) { }
d) void method(int numbers) { }
Answer: a
120. What is the output of the following code?
void printValue(double x) { System.out.println(x); }
// Called as: printValue(5.5);
a) 5.5
b) Error
c) No output
d) printValue
Answer: a

121. Which of the following is true about a method's return type?


a) It must always be void
b) It can be any primitive or reference type
c) It must be a primitive type
d) It cannot be an array
Answer: b

122. What is the output of the following code?


int getSum(int a, int b, int c) { return a + b + c; }
// Called as: System.out.println(getSum(4, 5, 6));
a) 15
b) 9
c) 6
d) Error
Answer: a

123. Can a method be declared with the 'strictfp' keyword?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

124. What is the output of the following code?


void showDouble(double x) { System.out.println(x * 2); }
// Called as: showDouble(4.5);
a) 9.0
b) 4.5
c) Error
d) No output
Answer: a

125. What is the purpose of the 'strictfp' keyword in a method?


a) To ensure strict floating-point calculations
b) To make the method static
c) To restrict access to the method
d) To allow multiple return types
Answer: a

126. What is the output of the following code?


int getMin(int a, int b) { return a < b ? a : b; }
// Called as: System.out.println(getMin(3, 7));
a) 3
b) 7
c) 10
d) Error
Answer: a

127. Can a method be called from a different class?


a) Yes, if it is accessible
b) No
c) Only if it is static
d) Only if it is void
Answer: a

128. What is the output of the following code?


void printCube(int n) { System.out.println(n * n * n); }
// Called as: printCube(3);
a) 27
b) 9
c) 3
d) Error
Answer: a

129. Which of the following is a valid method declaration with a String parameter?
a) void method(String s) { }
b) method String(s) { }
c) void method { String s; }
d) String method(s) { }
Answer: a
130. What is the output of the following code?
int multiplyThree(int a, int b, int c) { return a * b * c; }
// Called as: System.out.println(multiplyThree(2, 3, 4));
a) 24
b) 9
c) 4
d) Error
Answer: a

131. What happens if a method is declared with an invalid return type?


a) It runs normally
b) It causes a compilation error
c) It returns null
d) It skips the method
Answer: b

132. What is the output of the following code?


void printText(String text) { System.out.println(text); }
// Called as: printText("Learn Java");
a) Learn Java
b) Error
c) No output
d) printText
Answer: a

133. Can a method be declared with the 'native' keyword?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: a

134. What is the output of the following code?


int getDifference(int a, int b) { return a - b; }
// Called as: System.out.println(getDifference(10, 7));
a) 3
b) 17
c) 7
d) Error
Answer: a

135. What is the purpose of the 'native' keyword in a method?


a) To indicate the method is implemented in another language
b) To make the method static
c) To restrict access to the method
d) To allow multiple return types
Answer: a

136. What is the output of the following code?


void printDouble(double x) { System.out.println(x * 2); }
// Called as: printDouble(2.5);
a) 5.0
b) 2.5
c) Error
d) No output
Answer: a

137. Can a method have a return type of void and still use a return statement?
a) Yes
b) No
c) Only if it is static
d) Only if it has parameters
Answer: a

138. What is the output of the following code?


int getTriple(int n) { return n * 3; }
// Called as: System.out.println(getTriple(4));
a) 12
b) 4
c) 8
d) Error
Answer: a

139. Which of the following is true about method names in Java?


a) They must start with a number
b) They must follow identifier rules
c) They cannot contain underscores
d) They must be uppercase
Answer: b
140. What is the output of the following code?
void printName(String name) { System.out.println(name); }
// Called as: printName("John");
a) John
b) Error
c) No output
d) printName
Answer: a

141. Can a method be declared with multiple return types?


a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: b

142. What is the output of the following code?


int addFour(int a, int b, int c, int d) { return a + b + c + d; }
// Called as: System.out.println(addFour(1, 2, 3, 4));
a) 10
b) 6
c) 4
d) Error
Answer: a

143. What happens if a method is called with incompatible argument types?


a) It runs normally
b) It causes a compilation error
c) It returns null
d) It skips the method
Answer: b

144. What is the output of the following code?


void showNumber(double x) { System.out.println(x); }
// Called as: showNumber(3.14);
a) 3.14
b) Error
c) No output
d) showNumber
Answer: a

145. Which of the following is a valid method declaration with a float return type?
a) float getValue() { return 5.5f; }
b) getValue float() { return 5.5f; }
c) float getValue { return 5.5f; }
d) float() getValue { return 5.5f; }
Answer: a

146. What is the output of the following code?


int getQuotient(int a, int b) { return a / b; }
// Called as: System.out.println(getQuotient(15, 3));
a) 5
b) 18
c) 3
d) Error
Answer: a

147. Can a method be overloaded with different parameter names but the same types?
a) Yes
b) No
c) Only if it is static
d) Only if it is void
Answer: b

148. What is the output of the following code?


void printValue(int x) { System.out.println(x * 4); }
// Called as: printValue(5);
a) 20
b) 5
c) Error
d) No output
Answer: a

149. What is the purpose of the 'default' access modifier in a method?


a) To allow access from any class
b) To allow access within the same package
c) To restrict access to the same class
d) To make the method static
Answer: b
150. What is the output of the following code?
int getSquareRoot(int n) { return n * n; }
// Called as: System.out.println(getSquareRoot(5));
a) 25
b) 10
c) 5
d) Error
Answer: a

You might also like