Itemp Java Quiz 5

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Ekkoo
E
Ekkoo
Community Contributor
Quizzes Created: 4 | Total Attempts: 2,539
Questions: 18 | Attempts: 283

SettingsSettingsSettings
Java Quizzes & Trivia

Chapter 5 Revision


Questions and Answers
  • 1. 

    public class Converter {           public static void main(String args [ ]) {                    Converter con = new Converter();                    con.dollartoRupees(15);          }           double dollartoRupees(int dollar) {                     System.out.println (dollar+" dollar is equivalent to Rs" +dollar*25);            } } What will be the output of this program?  

    • A.

      Dollar is equivalent to Rs 375

    • B.

      15 dollar is equivalent to Rs 375

    • C.

      Dollar dollar is equivalent to Rs 375

    • D.

      This program gives a run time error

    Correct Answer
    B. 15 dollar is equivalent to Rs 375
    Explanation
    The output of this program will be "15 dollar is equivalent to Rs 375". This is because the main method calls the dollartoRupees method with the argument 15. Inside the dollartoRupees method, the value of the dollar parameter is multiplied by 25 and then printed along with the text "dollar is equivalent to Rs". So, when the dollartoRupees method is called with the argument 15, it will print "15 dollar is equivalent to Rs 375".

    Rate this question:

  • 2. 

    Arguments to methods always appear within __________.

    • A.

      Square brackets [ ]

    • B.

      Parentheses ( )

    • C.

      Curly braces { }

    • D.

      Quotation marks " "

    Correct Answer
    B. Parentheses ( )
    Explanation
    In programming, arguments to methods are enclosed within parentheses ( ). This is the standard syntax used to pass values or variables to a method for processing. The parentheses indicate that the enclosed values are being passed as arguments to the method, allowing the method to perform its intended functionality using those values. Square brackets [ ] are typically used for indexing or accessing elements in an array or list. Curly braces { } are used for defining blocks of code or declaring sets or dictionaries. Quotation marks " " are used to denote strings or text literals. Therefore, the correct answer is parentheses ( ).

    Rate this question:

  • 3. 

    Suppose your method does not return any value, which of the following keywords can be used as return type?

    • A.

      Void

    • B.

      Int

    • C.

      Double

    • D.

      Public

    Correct Answer
    A. Void
    Explanation
    The keyword "void" can be used as a return type when a method does not return any value. It is used to indicate that the method does not have a return statement and simply performs a task or action without returning a value.

    Rate this question:

  • 4. 

    An object is an instance of a __________.

    • A.

      Program

    • B.

      Class

    • C.

      Method

    • D.

      Data

    Correct Answer
    B. Class
    Explanation
    This question is asking about the definition of an object. In object-oriented programming, an object is an instance of a class. A class is a blueprint or template that defines the properties and behaviors of an object. Therefore, the correct answer is "class".

    Rate this question:

  • 5. 

    The keyword __________ is required to declare a class.

    • A.

      Public

    • B.

      Private

    • C.

      Class

    • D.

      All the above

    Correct Answer
    C. Class
    Explanation
    The keyword "class" is required to declare a class. In object-oriented programming, a class is a blueprint for creating objects. It defines the properties and behaviors that an object of that class will have. The "class" keyword is used to define a new class in a programming language. Therefore, the correct answer is "class".

    Rate this question:

  • 6. 

    Analyze the following code: class Test {    public static void main(String[] args){               System.out.println(findNumber(5));       }      public static int findNumber(int n) {               System.out.println("int");               return n;        }        public static long findNumber(long n) {                 System.out.println("long");                 return n;         } } What will happen when you execute this block of code?    

    • A.

      The program displays int followed by 5.

    • B.

      The program displays long followed by 5.

    • C.

      The program runs fine but displays things other than given in (A) and (B).

    • D.

      The program does not compile because the compiler cannot distinguish which findNumber to invoke.

    Correct Answer
    A. The program displays int followed by 5.
    Explanation
    The program displays int followed by 5 because the method findNumber with an int parameter is called when the argument passed is an int value. The method with a long parameter is not called because the argument is not a long value.

    Rate this question:

  • 7. 

    All Java applications must have a method called __________.

    • A.

      Main()

    • B.

      Init()

    • C.

      Main()

    • D.

      Init()

    Correct Answer
    C. Main()
    Explanation
    In Java, the main() method is the entry point for any Java application. It is the method that is called when the program starts running. Therefore, all Java applications must have a main() method.

    Rate this question:

  • 8. 

    A method can be declared with no parameters. TRUE or FALSE?

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    A method can indeed be declared with no parameters. In programming, parameters are used to pass information into a method. However, there are cases where a method does not require any input and can still perform its functionality. In such cases, the method can be declared with no parameters. This allows for flexibility in method design and implementation.

    Rate this question:

  • 9. 

    You must have a return statement in a non-void method. TRUE or FALSE?

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In Java, a non-void method is a method that returns a value. It is mandatory to have a return statement in a non-void method to specify the value that should be returned. If a return statement is missing, it will result in a compilation error. Therefore, the statement "You must have a return statement in a non-void method" is true.

    Rate this question:

  • 10. 

    You can declare two variables with the same name in __________.

    • A.

      A method

    • B.

      A block

    • C.

      Two nested blocks in a method

    • D.

      Different methods in a class

    Correct Answer
    D. Different methods in a class
    Explanation
    In Java, it is possible to declare two variables with the same name in different methods within a class. Each method has its own scope, so variables declared within a method are only accessible within that method. Therefore, it is allowed to have variables with the same name in different methods without any conflict or ambiguity.

    Rate this question:

  • 11. 

    Variables that are shared by every instances of a class are __________.

    • A.

      Public variables

    • B.

      Private variables

    • C.

      Instance variables

    • D.

      Class variables

    Correct Answer
    D. Class variables
    Explanation
    Class variables are variables that are shared by every instance of a class. They are declared at the class level and not inside any method or constructor. These variables belong to the class itself rather than any specific instance of the class. They can be accessed and modified by all instances of the class and are commonly used to store data that is shared among all instances, such as constants or counters.

    Rate this question:

  • 12. 

    A variable that is associated with an individual object is called __________.

    • A.

      A static variable

    • B.

      A class variable

    • C.

      An instance variable

    • D.

      An object reference variable

    Correct Answer
    C. An instance variable
    Explanation
    An instance variable is a variable that is associated with an individual object. It is unique to each instance of a class and holds different values for different objects. This allows each object to have its own set of data and characteristics.

    Rate this question:

  • 13. 

    When you write more than one method with the same name but different parameters in the same class, then it is called __________.

    • A.

      Overload

    • B.

      Override

    • C.

      Copy

    • D.

      Function

    Correct Answer
    A. Overload
    Explanation
    Overload is the correct answer because when you write more than one method with the same name but different parameters in the same class, it is called method overloading. Method overloading allows you to have multiple methods with the same name but different parameter lists, and the appropriate method is called based on the arguments provided when the method is invoked. This allows for more flexibility and versatility in programming by allowing the same method name to be used for different purposes.

    Rate this question:

  • 14. 

    What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?

    • A.

      Public

    • B.

      Private

    • C.

      Protected

    • D.

      Use the default modifier.

    Correct Answer
    D. Use the default modifier.
    Explanation
    The default modifier allows a class to be accessed by other classes within the same package but not by classes in different packages. This means that any class in the same package as the class with the default modifier can access it, while classes in different packages cannot.

    Rate this question:

  • 15. 

    When a new key word is used to create an instance of a class,

    • A.

      Java allocates memory for the object.

    • B.

      Initialises the instance variables.

    • C.

      Calls the constructor.

    • D.

      All the above

    Correct Answer
    D. All the above
    Explanation
    When a new keyword is used to create an instance of a class in Java, all of the above actions occur. Java first allocates memory for the object, then initializes the instance variables, and finally calls the constructor. These steps are necessary to properly create and initialize a new object of the class.

    Rate this question:

  • 16. 

    A constructor can be overloaded.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Overloading in programming refers to the ability to define multiple methods or functions with the same name but with different parameters. Similarly, a constructor can also be overloaded, meaning that multiple constructors can be defined for a class with different parameters. This allows for flexibility in creating objects of a class, as different constructors can be used depending on the specific requirements. Therefore, the statement "A constructor can be overloaded" is true.

    Rate this question:

  • 17. 

    A constructor always has a return type.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A constructor does not have a return type. It is a special method that is automatically called when an object is created, and its purpose is to initialize the object's state. Unlike regular methods, constructors do not have a return type specified, not even void.

    Rate this question:

  • 18. 

    The this key word is used to

    • A.

      Declare a variable

    • B.

      Initialize a variable

    • C.

      Refer to the current object

    • D.

      Create an object

    Correct Answer
    C. Refer to the current object
    Explanation
    The "this" keyword in programming is used to refer to the current object. It is typically used within a class or method to access the instance variables or methods of the current object. By using "this", we can differentiate between local variables and instance variables that have the same name. It helps in improving code readability and avoids confusion.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 15, 2011
    Quiz Created by
    Ekkoo
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.