Assignment 4
1. What is the purpose of an interface in Java?
a) To implement methods
b) To define class hierarchy
c) To provide method signatures without implementation
d) To inherit private members
2. Which keyword is used to implement an interface in Java?
a) extends
b) implements
c) interface
d) new
3. Can an interface have constructors?
a) Yes
b) No
4. Which of the following is true about interfaces?
a) They can be instantiated directly
b) They can have static methods
c) They can have instance variables
d) They can extend classes
5. How many interfaces can a class implement?
a) One
b) Two
c) Multiple
d) None
6. Which keyword is used to define an interface?
a) interface
b) class
c) abstract
d) implements
7. What is the default modifier for members of an interface?
a) public
b) protected
c) private
d) static
8. Can an interface extend another interface?
a) No
b) Yes
9. What is the result of compiling a class that implements an interface but does not
define all its methods?
a) Compile error
b) Runtime error
c) Warning
d) Success
10. Which is NOT allowed in a Java interface?
a) final variables
b) default methods
c) constructors
d) static methods
11. How can you access variables defined in an interface?
a) Using class object
b) Using interface name
c) Directly
d) Cannot access
12. What happens when an interface has two variables with the same name from
different interfaces?
a) Compile error
b) Ambiguity
c) Use interface name to qualify
d) Runtime exception
13. What kind of variables can be declared in an interface?
a) Instance variables
b) Public static final
c) Protected
d) Private
14. Interfaces in Java support which type of inheritance?
a) Single
b) Multilevel
c) Multiple
d) Hybrid
15. Which of these is a valid interface declaration?
a) interface MyInterface {}
b) class interface MyInterface {}
c) implements MyInterface {}
d) interface = MyInterface {}
16. What is the primary use of packages in Java?
a) Faster execution
b) Memory management
c) Code organization and namespace management
d) Garbage collection
17. Which keyword is used to define a package?
a) import
b) class
c) package
d) extends
18. What is the default package in Java if no package is specified?
a) java.lang
b) user-defined
c) anonymous package
d) default package
19. Which statement is used to include a package in a class?
a) include
b) package
c) import
d) extends
20. What does the java.lang package contain?
a) GUI classes
b) Utility classes
c) Core classes like String and Math
d) Database classes
21. What is the naming convention for user-defined packages?
a) Capitalized words
b) Mixed-case
c) Lowercase
d) Numbers only
22. How do you access a class from a different package?
a) Using import statement
b) Using new keyword
c) By subclassing
d) By implementing
23. What is true about system packages in Java?
a) Created by the user
b) Cannot be used
c) Part of Java API
d) Stored in temp folders
24. How do you hide a class in a package from being used by others?
a) Make it private
b) Make it protected
c) Do not declare it public
d) Use abstract
25. Which of these packages is automatically imported by every Java class?
a) java.io
b) java.util
c) java.lang
d) java.net
26. How can you add a class to an existing package?
a) Add to import
b) Add package statement at top
c) Compile in a different directory
d) Use interface
27. What does the statement import java.util.*; do?
a) Imports all Java packages
b) Imports all classes in java.util
c) Imports sub-packages
d) Imports a specific class
28. What happens if you try to import a non-existent package?
a) Runtime error
b) Compile-time error
c) Ignored
d) Warning
29. Which of the following allows creating reusable code libraries?
a) Threads
b) Packages
c) Interfaces
d) Exceptions
30. Which command is used to compile a Java file into a specific package
directory?
a) javac filename.java
b) javac -d . filename.java
c) java filename.java
d) javap filename.java
31. Which class is used to create a thread in Java?
a) Runnable
b) Thread
c) Executor
d) Process
32. What method must be defined when implementing Runnable?
a) execute()
b) run()
c) start()
d) begin()
33. Which method starts a thread?
a) run()
b) start()
c) begin()
d) launch()
34. What happens if you call run() instead of start()?
a) Thread starts in new thread
b) Compile error
c) Thread runs in current thread
d) Exception is thrown
35. How can a thread be temporarily stopped?
a) pause()
b) stop()
c) wait()
d) sleep()
36. Which method can you use to synchronize a block of code?
a) sync()
b) synchronized()
c) synchronized block
d) lock()
37. What is the default thread priority?
a) 0
b) 1
c) 5
d) 10
38. Which method checks if a thread is alive?
a) isStarted()
b) alive()
c) isAlive()
d) checkAlive()
39. What exception is thrown when sleep() is interrupted?
a) InterruptedException
b) ThreadException
c) SleepException
d) Exception
40. What is the purpose of synchronization in threads?
a) To improve speed
b) To reduce memory
c) To prevent race conditions
d) To reduce CPU usage