0% found this document useful (0 votes)
33 views3 pages

Java Programming Answer Key 2024-25

The document contains an answer key for a Java Programming course exam at Shri G.P.M. Degree College of Science & Commerce. It includes 20 multiple-choice questions along with the correct answers related to Java programming concepts. The topics cover constants, data types, constructors, methods, threads, exceptions, and more.

Uploaded by

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

Java Programming Answer Key 2024-25

The document contains an answer key for a Java Programming course exam at Shri G.P.M. Degree College of Science & Commerce. It includes 20 multiple-choice questions along with the correct answers related to Java programming concepts. The topics cover constants, data types, constructors, methods, threads, exceptions, and more.

Uploaded by

amangupta5123j
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Smt.

Shyampatidevi Mishra Educational Trust’s


SHRI G.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE
Affiliated to University of Mumbai [Link] shrigpm@[Link]
Rajarshi Shahu Maharaj Road, Andheri (E), Mumbai – 400069. ✆ : 8928387197 / 8
Add on / Certificate Course : 2024 – 25
Answer Key
Course Name : Java Programming Department : IT
College Coordinator’s Name : Prof. Sahil Wade
Exam ____________ Max. Marks 20

Q. No.
1. Which of the following is used to define a constant in Java?
a) final
b) const
c) static
d) constant
Answer: a) final

2. What is the default value of a boolean variable in Java?


a) true
b) false
c) 0
d) null
Answer: b) false

3. Which of the following data types cannot be used to create an array in Java?
a) int
b) double
c) boolean
d) void
Answer: d) void

4. What is the size of an int in Java?


a) 16 bits
b) 32 bits
c) 64 bits
d) 128 bits
Answer: b) 32 bits

5. Which of these statements is true about constructors in Java?


a) A constructor has a return type.
b) A constructor cannot have parameters.
c) A constructor is called when an object is created.
d) A constructor must always be public.
Answer: c) A constructor is called when an object is created.

6. Which keyword is used to inherit a class in Java?


a) extends
b) inherits
c) super
d) implements
Answer: a) extends

7. Which of the following is a valid method signature in Java?


a) int 1method()
b) void method()!
c) boolean method(int x)
d) float method()&
Answer: c) boolean method(int x)
8. What is the output of the following Java code?
[Link](3 + 2 + "Hello");
a) 5Hello
b) Hello5
c) Hello32
d) 32Hello
Answer: a) 5Hello

9. Which method is used to start a thread execution in Java?


a) run()
b) start()
c) init()
d) execute()
Answer: b) start()

10. Which of the following classes is a subclass of Exception in Java?


a) Error
b) IOException
c) Throwable
d) RuntimeException
Answer: b) IOException

11. What does the final keyword indicate in Java?


a) The method can be overridden.
b) The variable's value cannot be modified.
c) The class can be subclassed.
d) The method can throw an exception.
Answer: b) The variable's value cannot be modified.

12. Which of the following is the parent class of all classes in Java?
a) Object
b) Class
c) Throwable
d) String
Answer: a) Object

13. What is the default value of an instance variable of type String in Java?
a) ""
b) null
c) 0
d) undefined
Answer: b) null

14. Which of the following statements about Java interfaces is false?


a) An interface can have methods with a body.
b) An interface can extend another interface.
c) A class can implement multiple interfaces.
d) An interface cannot be instantiated.
Answer: a) An interface can have methods with a body.

15. Which of the following methods is used to get the length of a string in Java?
a) length()
b) size()
c) getLength()
d) count()
Answer: a) length()

16. What is the output of the following Java code?


[Link](10 / 4);
a) 2.5
b) 2
c) 2.0
d) Error
Answer: b) 2

17. Which package is automatically imported in every Java program?


a) [Link]
b) [Link]
c) [Link]
d) [Link]
Answer: b) [Link]

18. Which of the following operators is used to check equality in Java?


a) =
b) ==
c) ===
d) !=
Answer: b) ==

19. What does the super keyword refer to in Java?


a) The current class instance
b) The parent class
c) The current method
d) The current package
Answer: b) The parent class

20. Which of the following is the correct way to create an array of 10 integers in Java?
a) int[] arr = new int[10];
b) int arr = new int(10);
c) int arr = int[10];
d) int[10] arr = new int[];
Answer: a) int[] arr = new int[10];

Common questions

Powered by AI

In Java, the 'extends' clause for classes allows one class to inherit fields and methods from another class, supporting code reuse and polymorphic behavior. Interface extension, on the other hand, permits an interface to inherit method signatures from another interface without providing implementations. While a class is limited to extending one other class due to single inheritance, an interface can extend multiple interfaces, which facilitates a form of multiple inheritance in behavior definition .

In Java, 'extends' is used to indicate inheritance between classes, enabling a class to inherit fields and methods from another class . The 'implements' keyword, however, is used by a class to fulfill the contract of an interface, thereby providing implementations for its abstract methods. While a class can only extend one other class (single inheritance), it can implement multiple interfaces, thus allowing polymorphism and multiple inheritance of type .

Java's default package import mechanism enhances program simplicity by automatically importing 'java.lang', which contains fundamental classes and interfaces used frequently in Java programs, such as String and System . This automatic inclusion means developers need not explicitly import these foundational components, streamlining the coding process and reducing the need for repetitive import statements, thereby enhancing development efficiency.

The 'final' keyword in Java serves different purposes depending on where it is used. For variables, it indicates that the variable's value cannot be modified once initialized . For methods, 'final' means that the method cannot be overridden by subclasses. For classes, it implies that the class cannot be subclassed, preventing any extension or modification of its behavior.

Java ensures type safety and identifies incorrect method calls through strict method signature rules. These signatures include elements such as method names, parameter types, and return types. A valid method signature must conform to Java's syntax and naming rules, for instance, method names cannot start with a digit, ensuring that methods are properly identified and called with the correct parameters . This prevents runtime errors by catching mismatched calls at compile time.

The 'Object' class is the root of Java's class hierarchy and is considered fundamental because every class in Java implicitly inherits from it. This makes it an ancestor of all other classes, providing essential methods like equals(), hashCode(), and toString() that can be overridden as necessary . This design ensures a consistent API for operations such as object comparison and string conversion across the entire Java ecosystem.

The distinction between compile-time and runtime operations in Java is critical for understanding how code is executed. Arithmetic operations, for example, behave differently based on operand types, which affects compile-time type checking and runtime execution. Integer division, such as 10 / 4, results in an integer at compile-time, yielding 2, without considering fractional parts . This contrast highlights Java's compile-time type checking, ensuring type safety, and runtime execution, where data types dictate how operations are performed.

In Java, default values for primitive data types are provided automatically during object instantiation. For instance, the default value of a boolean variable is false . For reference types like String, the default value is null . This means that if a variable is defined but not initialized explicitly, Java assigns these default values automatically.

Java's thread execution model is central to its concurrent programming capabilities. The 'start()' method is pivotal as it initiates a new thread, invoking the 'run()' method in a separate execution path . This model allows multiple processes to run concurrently, supporting efficient multitasking and resource utilization. The separation of 'start()' and 'run()' methods ensures that threads are ready-to-run upon execution, facilitating smooth thread lifecycle management and complex concurrent operations.

Java constructors differ from regular methods primarily in that they lack a return type and are invoked automatically when an object is created. They share the class name and are used to initialize new objects, setting initial values for instance variables or executing startup procedures . Unlike methods, constructors cannot be called once the object is created and they play a crucial role in defining how objects are constructed and prepared for use.

You might also like