FliQi Education App Download
प्रश्न-बैंक · Programmer Java

OOPs Concepts - Methods in Java — प्रश्न और उत्तर

Programmer Java · 22 जाँचे हुए (verified) प्रश्न

  • 22 जाँचे हुए प्रश्न
  • हिंदी + English

पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 12 के उत्तर और हल FliQi Question Bank app में।

भाषा
प्रश्न 1उत्तर + हल
निम्नलिखित मेथड डिक्लेरेशन पर विचार कीजिए:

public void calculateSum(double... numbers) {

    double sum = 0;

    for (double num : numbers) {

        sum += num;

    }

    System.out.println("Sum: " + sum);

}

निम्नलिखित मेथड कॉल का आउटपुट क्या होगा?

calculateSum(1.5, 2.5, 3.5);
Consider the following method declaration:

public void calculateSum(double... numbers) {

    double sum = 0;

    for (double num : numbers) {

        sum += num;

    }

    System.out.println("Sum: " + sum);

}

What will be the output of the following method call?

calculateSum(1.5, 2.5, 3.5);
  1. ASum: 7.5
  2. B7.5
  3. Cत्रुटि, मेथड कॉल ग़लत है।Error, method call is incorrect.
  4. DSum: 1.5 2.5 3.5

सही उत्तर / Answer: (A)

हल / Solution
calculateSum का पैरामीटर double... numbers है, यानी यह कितने भी double मान ले सकता है। कॉल में तीन मान 1.5, 2.5 और 3.5 दिए गए हैं, इसलिए कॉल सही है। for-each लूप इन्हें जोड़ता है: 1.5 + 2.5 + 3.5 = 7.5। println में "Sum: " के साथ योग जुड़कर छपता है, इसलिए आउटपुट Sum: 7.5 है। केवल 7.5 में आगे का पाठ छूट गया है, और अंतिम विकल्प में जोड़ हुआ ही नहीं।
The parameter of calculateSum is double... numbers, so it accepts any number of double values. The call passes three values, 1.5, 2.5 and 3.5, so it is valid. The for-each loop adds them: 1.5 + 2.5 + 3.5 = 7.5. println joins the text "Sum: " with the total, so the output is Sum: 7.5. Plain 7.5 misses the label, and the last option shows no addition at all.
प्रश्न 2उत्तर + हल
varargs मेथड के लिए निम्नलिखित में से कौन-सा डिक्लेरेशन अमान्य है?
Which of the following method declarations is invalid for a varargs method?
  1. Avoid display(int... values, String message)
  2. Bvoid display(String message, int... values)
  3. Cvoid display(String... messages)
  4. Dvoid display()

सही उत्तर / Answer: (A)

हल / Solution
नियम है कि किसी मेथड में varargs पैरामीटर केवल एक हो सकता है और वह सबसे अंत में होना चाहिए। display(int... values, String message) में varargs के बाद String पैरामीटर आया है, इसलिए javac त्रुटि देता है। display(String message, int... values) में varargs अंत में है, इसलिए यह मान्य है। display(String... messages) भी मान्य है, और display() एक सामान्य मान्य डिक्लेरेशन है। इसलिए उत्तर विकल्प 1 है।
The rule is that a method can have only one varargs parameter, and it must be the last one. In display(int... values, String message) a String parameter follows the varargs, so javac reports an error. In display(String message, int... values) the varargs comes last, so it is valid. display(String... messages) is also valid, and display() is an ordinary legal declaration. So the invalid one is option 1.
प्रश्न 3उत्तर + हल
कई integer आर्ग्युमेंट लेने वाले printValues नामक varargs मेथड के लिए निम्नलिखित में से कौन-सा डिक्लेरेशन मान्य है?
Which of the following method declarations is valid for a varargs method named printValues that takes multiple integers as arguments?
  1. Avoid printValues(int... numbers)
  2. Bvoid printValues(int[] numbers)
  3. Cvoid printValues(int numbers...)
  4. Dvoid printValues(int... numbers[])

सही उत्तर / Answer: (A)

हल / Solution
varargs पैरामीटर लिखने का सही रूप है: टाइप, फिर तीन बिंदु, फिर नाम, यानी int... numbers। इसलिए विकल्प 1 सही है। int[] numbers मान्य array पैरामीटर है, पर varargs नहीं; इसे printValues(1, 2, 3) की तरह नहीं बुलाया जा सकता। int numbers... में बिंदु ग़लत जगह हैं। int... numbers[] पर javac त्रुटि देता है, क्योंकि varargs पैरामीटर के नाम के बाद [] लगाना मना है।
The correct form of a varargs parameter is type, then three dots, then the name: int... numbers. So option 1 is correct. int[] numbers is a valid array parameter but not varargs, so printValues(1, 2, 3) cannot be called with it. In int numbers... the dots are in the wrong place. For int... numbers[] javac reports an error, because array brackets after a varargs parameter name are not allowed.
प्रश्न 4उत्तर + हल
मेथड डिक्लेरेशन में varargs पैरामीटर दर्शाने के लिए किस चिह्न (symbol) का उपयोग होता है?
Which symbol is used to denote a varargs parameter in a method declaration?
  1. Avarargs
  2. Bvar
  3. Cargs
  4. D...

सही उत्तर / Answer: (D)

हल / Solution
varargs पैरामीटर बनाने के लिए डेटा टाइप के बाद तीन बिंदु (...) लगाए जाते हैं, जैसे void show(String... names)। यह ellipsis नाम का चिह्न है, कोई कीवर्ड नहीं। varargs जावा में कीवर्ड नहीं, बस इस सुविधा का नाम है। var का उपयोग लोकल वेरिएबल का टाइप अपने आप तय करवाने में होता है। args तो बस एक आम पैरामीटर नाम है, जैसे main(String[] args) में। उत्तर ... है।
To create a varargs parameter, three dots (...) are placed after the data type, as in void show(String... names). This is the ellipsis symbol, not a keyword. varargs is only the name of the feature, not a Java keyword. var is used for local variable type inference. args is just a common parameter name, as in main(String[] args). So the answer is the ellipsis, written as three dots.
प्रश्न 5उत्तर + हल
जावा में varargs मेथड क्या है?
What is a varargs method in Java?
  1. Aऐसा मेथड जो तय संख्या में आर्ग्युमेंट लेता है।A method that takes a fixed number of arguments.
  2. Bऐसा मेथड जो बदलती (variable) संख्या में आर्ग्युमेंट लेता है।A method that takes a variable number of arguments.
  3. Cऐसा मेथड जो कोई बदलता मान लौटाता है।A method that returns a variable value.
  4. Dऐसा मेथड जिसे overload नहीं किया जा सकता।A method that cannot be overloaded.

सही उत्तर / Answer: (B)

हल / Solution
varargs का पूरा नाम variable arguments है। ऐसा मेथड एक ही पैरामीटर के ज़रिए शून्य, एक या कई आर्ग्युमेंट ले सकता है, जैसे void sum(int... nums)। मेथड के अंदर ये आर्ग्युमेंट एक array की तरह मिलते हैं। तय संख्या वाला मेथड सामान्य मेथड है। varargs का रिटर्न मान से कोई लेना-देना नहीं। varargs मेथड overload भी किए जा सकते हैं। इसलिए सही उत्तर विकल्प 2 है।
Varargs stands for variable arguments. Such a method can accept zero, one or many arguments through a single parameter, for example void sum(int... nums). Inside the method these arguments are available as an array. A method with a fixed number of arguments is an ordinary method. Varargs has nothing to do with the return value, and varargs methods can be overloaded too. So option 2 is correct.
प्रश्न 6उत्तर + हल

 

मेथड डिक्लेरेशन में void कीवर्ड का क्या उद्देश्य है?
What is the purpose of the void keyword in a method declaration?
  1. Aयह बताता है कि मेथड कोई मान नहीं लौटाता।It specifies that the method returns no value.
  2. Bयह दर्शाता है कि मेथड को override नहीं किया जा सकता।It indicates that the method cannot be overridden.
  3. Cयह बताता है कि मेथड को क्लास के बाहर से नहीं बुलाया जा सकता।It specifies that the method cannot be called from outside the class.
  4. Dइसका उपयोग मेथड को abstract घोषित करने के लिए होता है।It is used to declare a method as abstract.

सही उत्तर / Answer: (A)

हल / Solution
void रिटर्न टाइप की जगह लिखा जाता है और बताता है कि मेथड कोई मान वापस नहीं भेजता। इसलिए विकल्प 1 सही है। मेथड को override होने से रोकने के लिए final लगाया जाता है। क्लास के बाहर से बुलाए जाने से रोकने के लिए private लगाया जाता है। किसी मेथड को abstract बनाने के लिए abstract कीवर्ड चाहिए। void का इन तीनों बातों से कोई संबंध नहीं है।
void is written in place of the return type and tells us that the method sends no value back. So option 1 is correct. To stop a method from being overridden, final is used. To stop it from being called outside the class, private is used. To declare a method abstract, the abstract keyword is needed. void has nothing to do with any of these three, so options 2, 3 and 4 are wrong.
प्रश्न 7उत्तर + हल
जावा में मेथड के बारे में निम्नलिखित में से कौन-सा कथन सही है?
Which of the following statements about methods in Java is true?
  1. Aमेथड कोई मान नहीं लौटा सकते।Methods cannot return values.
  2. Bमेथड हमेशा static घोषित होने चाहिए।Methods must always be declared as static.
  3. Cमेथड को दूसरे मेथड से बुलाया जा सकता है।Methods can be called from other methods.
  4. Dमेथड पैरामीटर नहीं ले सकते।Methods cannot accept parameters.

सही उत्तर / Answer: (C)

हल / Solution
जावा में एक मेथड के अंदर से दूसरे मेथड को बुलाना बिलकुल सामान्य है; main() से ही प्रोग्राम के बाक़ी मेथड बुलाए जाते हैं। इसलिए विकल्प 3 सही है। बाक़ी कथन ग़लत हैं। मेथड return से कोई मान लौटा सकते हैं। मेथड का static होना ज़रूरी नहीं, ज़्यादातर मेथड इंस्टेंस मेथड होते हैं। और मेथड पैरामीटर भी ले सकते हैं, जैसे add(int a, int b)।
In Java it is perfectly normal for one method to call another; main() itself calls the other methods of a program. So option 3 is true. The other statements are false. Methods can return values using return. Methods need not be static; most methods are instance methods. Methods can also accept parameters, for example add(int a, int b). Hence the correct statement is that methods can be called from other methods.
प्रश्न 8उत्तर + हल

 

जावा में मेथड क्या है?
What is a method in Java?
  1. Aएक वेरिएबल जो किसी मान को दर्शाता है।A variable that represents a value.
  2. Bलूप के लिए इस्तेमाल होने वाला कीवर्ड।A keyword used for looping.
  3. Cकोड का एक नाम वाला खंड जो कोई ख़ास काम करता है।A named block of code that performs a specific task.
  4. Dइनहेरिटेंस के लिए इस्तेमाल होने वाली एक ख़ास तरह की क्लास।A special type of class used for inheritance.

सही उत्तर / Answer: (C)

हल / Solution
जावा में मेथड कोड का एक नाम वाला खंड (block) है, जो कोई ख़ास काम करता है। इसे नाम से बुलाया जाता है, यह पैरामीटर ले सकता है और कोई मान लौटा सकता है। वेरिएबल मान रखता है, मेथड नहीं। for, while जैसे कीवर्ड लूप बनाते हैं। इनहेरिटेंस के लिए अलग तरह की क्लास नहीं होती, कोई भी सामान्य क्लास extends से आगे बढ़ाई जा सकती है। सही उत्तर विकल्प 3 है।
In Java, a method is a named block of code that performs a specific task. It is invoked by its name, may take parameters and may return a value. A variable holds a value, which a method does not. Keywords such as for and while create loops. There is no special class just for inheritance; any ordinary class can be extended with extends. So option 3 is correct.
प्रश्न 9उत्तर + हल
किसी ऑब्जेक्ट में मौजूद सबरूटीन को _____ मेथड कहा जाता है।
The subroutines contained in an object are called _____ methods.
  1. Aक्लास मेथडClass Methods
  2. Bस्टैटिक मेथडStatic Methods
  3. Cइंस्टेंस मेथडInstance Methods
  4. Dइंटरफ़ेस मेथडInterface Methods

सही उत्तर / Answer: (C)

हल / Solution
जावा में मेथड को सबरूटीन भी कहा जाता है। जो मेथड static नहीं होते, वे हर ऑब्जेक्ट से जुड़े होते हैं और उसी ऑब्जेक्ट के डेटा पर काम करते हैं, इसलिए उन्हें इंस्टेंस मेथड कहते हैं। क्लास मेथड और स्टैटिक मेथड एक ही चीज़ हैं; वे क्लास के होते हैं, किसी ऑब्जेक्ट के नहीं। इंटरफ़ेस मेथड interface में घोषित होते हैं। इसलिए सही उत्तर इंस्टेंस मेथड है।
In Java, methods are also called subroutines. Methods that are not static belong to each object and work on that object's own data, so they are called instance methods. Class methods and static methods are the same thing; they belong to the class itself rather than to any object. Interface methods are declared inside an interface. So the subroutines contained in an object are instance methods, option 3.
प्रश्न 10उत्तर + हल
जावा में लोकल वेरिएबल __ मेमोरी में और इंस्टेंस वेरिएबल ___ मेमोरी में संग्रहीत होते हैं।
In Java, local variables are stored in __ memory and instance variables are stored in ___ memory.
  1. AStack, Stack
  2. BHeap, Heap
  3. CStack, Heap
  4. DHeap, Stack

सही उत्तर / Answer: (C)

हल / Solution
जब कोई मेथड चलता है तो उसके लिए stack पर एक frame बनता है। उसी frame में उस मेथड के लोकल वेरिएबल और पैरामीटर रहते हैं, और मेथड पूरा होते ही हट जाते हैं। इंस्टेंस वेरिएबल ऑब्जेक्ट का हिस्सा होते हैं, और new से बना हर ऑब्जेक्ट heap में रहता है। इसलिए इंस्टेंस वेरिएबल heap में होते हैं। क्रम उलटने वाले या दोनों को एक जगह रखने वाले विकल्प ग़लत हैं। उत्तर Stack, Heap है।
When a method runs, a frame is created for it on the stack. Its local variables and parameters live in that frame and disappear when the method returns. Instance variables are part of an object, and every object created with new lives on the heap. So instance variables are stored in heap memory. Options that reverse the order or put both in the same area are wrong. The answer is Stack, Heap.
आगे के 12 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 14उत्तर app में
MathUtility नामक क्लास के calculate नामक स्टैटिक मेथड को कॉल करने का सही तरीका निम्नलिखित में से कौन-सा है?
Which of the following is a correct way to call a static method named calculate from a class named MathUtility?
  1. Acalculate.MathUtility()
  2. Bnew MathUtility().calculate()
  3. CMathUtility.calculate()
  4. Dcalculate()
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 15उत्तर app में
मेथड के पैरामीटर का मूल्यांकन (evaluation) कब होता है?
When are method parameters evaluated?
  1. Aकंपाइल टाइम परAt compile time
  2. Bरनटाइम परAt runtime
  3. Cलोड टाइम परAt load time
  4. Dमेथड पूरा होने के बादAfter method completion
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 16उत्तर app में
निम्नलिखित में से कौन-सा जावा में मेथड का उद्देश्य नहीं है?
Which of the following is not a purpose of methods in Java?
  1. Aकोड की पुन: उपयोगिता (reusability) बढ़ानाIncrease code reusability
  2. Bसंरचित और व्यवस्थित तरीका देनाProvide a structured and organized approach
  3. Cडेटा संग्रहीत करनाStore data
  4. Dजटिल समस्या को सरल भागों में बाँटनाBreak a complex problem into simpler ones
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 17उत्तर app में
मेथड सिग्नेचर में क्या-क्या शामिल होता है?
What does the method signature consist of?
  1. Aकेवल मेथड का नामMethod name only
  2. Bमेथड का नाम और पैरामीटरMethod name and parameters
  3. Cमेथड का नाम, पैरामीटर और रिटर्न टाइपMethod name, parameters, and return type
  4. Dमेथड का नाम और रिटर्न टाइपMethod name and return type
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 20उत्तर app में
निम्नलिखित जावा प्रोग्राम का आउटपुट क्या होगा?

    class area 

    {

        int width;

        int length;

        int volume;

        area() 

        {

           width=5;

           length=6;

        }

        void volume() 

        {

             volume = width*length*height;

        } 

    }    

    class cons_method 

    {

        public static void main(String args[])

        {

            area obj = new area();

            obj.volume();

            System.out.println(obj.volume);        

        } 

    }
What will be the output of the following Java program?

    class area 

    {

        int width;

        int length;

        int volume;

        area() 

        {

           width=5;

           length=6;

        }

        void volume() 

        {

             volume = width*length*height;

        } 

    }    

    class cons_method 

    {

        public static void main(String args[])

        {

            area obj = new area();

            obj.volume();

            System.out.println(obj.volume);        

        } 

    }
  1. A0
  2. B1
  3. C30
  4. Dत्रुटि (error)error
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 22उत्तर app में
क्या जावा में एक एब्स्ट्रैक्ट क्लास में नॉन-एब्स्ट्रैक्ट (कंक्रीट) मेथड हो सकते हैं?
Can an abstract class have non-abstract (concrete) methods in Java?
  1. Aनहीं, एब्स्ट्रैक्ट क्लास में केवल एब्स्ट्रैक्ट मेथड ही हो सकते हैं।No, an abstract class can only have abstract methods.
  2. Bहाँ, पर केवल तब जब नॉन-एब्स्ट्रैक्ट मेथड “final” घोषित हों।Yes, but only if the non-abstract methods are marked as “final.”
  3. Cहाँ, एब्स्ट्रैक्ट क्लास में एब्स्ट्रैक्ट और नॉन-एब्स्ट्रैक्ट दोनों तरह के मेथड हो सकते हैं।Yes, an abstract class can have both abstract and non-abstract methods.
  4. Dहाँ, पर केवल तब जब नॉन-एब्स्ट्रैक्ट मेथड का access private हो।Yes, but only if the non-abstract methods have private access.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
OOPs Concepts - Methods in Java के सभी 22 प्रश्न — उत्तर, हल और अभ्यास

FliQi Question Bank app में topic-वार अभ्यास, कठिनाई-स्तर और आपकी प्रगति।

  • पहला test मुफ़्त
  • हिंदी + English
  • जाँचे हुए उत्तर और हल

यह topic किन परीक्षाओं में आता है

Programmer Java के दूसरे topics

सभी topics