0% found this document useful (0 votes)
69 views8 pages

Java Objective Questions and Answers

Uploaded by

rajsaini088
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)
69 views8 pages

Java Objective Questions and Answers

Uploaded by

rajsaini088
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

50 Objective Questions Based on Java Syllabus

1. Basics and Language Fundamentals

1. What does JVM stand for?


a) Java Verified Machine
b) Java Virtual Machine
c) Java Variable Manager
d) Java Vision Mode
Answer: b) Java Virtual Machine
2. Which of these is NOT a Java feature?
a) Platform-independent
b) Object-oriented
c) Compiled and interpreted
d) Non-secure
Answer: d) Non-secure
3. Which keyword is used to declare a class in Java?
a) class
b) new
c) define
d) struct
Answer: a) class
4. What is the default value of a boolean variable in Java?
a) true
b) false
c) null
d) undefined
Answer: b) false
5. Which operator is used for logical AND in Java?
a) &
b) &&
c) ||
d) and
Answer: b) &&
6. Which of these literals is invalid in Java?
a) 123.456
b) 'abc'
c) "hello"
d) true
Answer: b) 'abc'
7. Which Java method is the entry point of any Java program?
a) init()
b) main()
c) start()
d) execute()
Answer: b) main()
8. Typecasting in Java can be categorized into how many types?
a) 1
b) 2
c) 3
d) 4
Answer: b) 2 (Widening and Narrowing)
9. What is the use of the final keyword in Java?
a) To create constants
b) To prevent inheritance
c) To prevent method overriding
d) All of the above
Answer: d) All of the above
10. Which of the following is a primitive data type?
a) String
b) Integer
c) int
d) Array
Answer: c) int

2. Declarations, Control Structures, Arrays

11. What does the break statement do in Java?


a) Terminates the program
b) Skips the current iteration
c) Exits the loop
d) Restarts the loop
Answer: c) Exits the loop
12. Which loop is guaranteed to execute at least once?
a) for
b) while
c) do-while
d) foreach
Answer: c) do-while
13. Which is the correct syntax for a switch statement?
a) switch(expression) { case value: statements; break; }
b) switch(expression) { if case value: statements }
c) case switch { value, statements }
d) switch { value: case statements; }
Answer: a) switch(expression) { case value: statements; break; }
14. How do you declare a single-dimensional array in Java?
a) int[] arr;
b) int arr[];
c) Both a and b
d) None of the above
Answer: c) Both a and b
15. Which method is used to find the length of an array in Java?
a) size()
b) getSize()
c) length()
d) length
Answer: d) length
16. What is an anonymous array in Java?
a) An array without a name
b) An array declared without a size
c) An array declared using a loop
d) None of the above
Answer: a) An array without a name
17. Which of these is NOT a valid control structure?
a) if-else
b) repeat-until
c) switch
d) for
Answer: b) repeat-until
18. What is the default value of an array of integers?
a) null
b) 0
c) undefined
d) -1
Answer: b) 0
19. Which method is used to append a string to a StringBuffer in Java?
a) add()
b) concat()
c) append()
d) insert()
Answer: c) append()
20. Which keyword is used to declare an enumerated type?
a) enum
b) enumeration
c) type
d) class
Answer: a) enum

3. Classes, Interfaces, and Access Control

21. What does the this keyword refer to in Java?


a) The current object
b) The parent class
c) The method
d) None of the above
Answer: a) The current object
22. Which of these is NOT an access modifier in Java?
a) public
b) private
c) secure
d) protected
Answer: c) secure
23. How do you define a method in a Java class?
a) [visibility] returnType methodName(parameters) { body }
b) methodName(parameters) { body }
c) define methodName(parameters) { body }
d) None of the above
Answer: a) [visibility] returnType methodName(parameters) { body }
24. What is method overloading?
a) Defining multiple methods with the same name but different parameters
b) Writing methods that call other methods
c) Rewriting an inherited method in a subclass
d) Defining a method outside the class
Answer: a) Defining multiple methods with the same name but different parameters
25. What is the purpose of the super keyword in Java?
a) To call a parent class constructor or method
b) To create a superclass
c) To define a static member
d) None of the above
Answer: a) To call a parent class constructor or method

(Continued in next part...)

50 Objective Questions (Continued)

4. Applets and AWT Controls

26. Which method initializes an applet?


a) start()
b) init()
c) paint()
d) run()
Answer: b) init()
27. What is the purpose of the Graphics class in applets?
a) Handling input events
b) Performing arithmetic calculations
c) Drawing shapes and images
d) Managing applet lifecycle
Answer: c) Drawing shapes and images
28. Which tag is used to embed an applet in an HTML file?
a) <embed>
b) <applet>
c) <object>
d) <script>
Answer: b) <applet>
29. Which AWT component is used to display a single line of editable text?
a) Label
b) TextField
c) TextArea
d) Button
Answer: b) TextField
30. Which layout manager arranges components in a single row or column?
a) FlowLayout
b) GridLayout
c) BorderLayout
d) BoxLayout
Answer: d) BoxLayout
31. What event is triggered when a button is clicked in Java AWT?
a) ActionEvent
b) ItemEvent
c) MouseEvent
d) KeyEvent
Answer: a) ActionEvent
32. What does the setLayout() method do in Java?
a) Defines the size of the window
b) Sets the layout manager for a container
c) Configures the applet lifecycle
d) Specifies the font style
Answer: b) Sets the layout manager for a container
33. Which class in AWT is used for creating menus?
a) MenuBar
b) MenuItem
c) Menu
d) All of the above
Answer: d) All of the above
34. Which component allows the user to select one option from a drop-down list?
a) Choice
b) List
c) CheckboxGroup
d) Scrollbar
Answer: a) Choice
35. What method in the Applet class is used to display a string?
a) print()
b) println()
c) drawString()
d) writeText()
Answer: c) drawString()

5. Exception Handling, Multithreading, I/O Streams

36. Which keyword is used to handle exceptions in Java?


a) try
b) catch
c) throw
d) All of the above
Answer: d) All of the above
37. What is the purpose of the finally block?
a) To terminate the program
b) To execute code after a try-catch block, regardless of an exception
c) To handle runtime errors
d) None of the above
Answer: b) To execute code after a try-catch block, regardless of an exception
38. Which of the following exceptions is unchecked?
a) IOException
b) NullPointerException
c) FileNotFoundException
d) SQLException
Answer: b) NullPointerException
39. What is the default priority of a thread in Java?
a) 0
b) 1
c) 5
d) 10
Answer: c) 5
40. Which interface must be implemented to create a thread in Java?
a) Thread
b) Runnable
c) Callable
d) ThreadGroup
Answer: b) Runnable
41. What is the purpose of the synchronized keyword in Java?
a) To enable parallel execution
b) To prevent race conditions in multithreaded programs
c) To terminate threads
d) To increase thread priority
Answer: b) To prevent race conditions in multithreaded programs
42. Which method in Java reads a single character from a file?
a) readLine()
b) read()
c) getChar()
d) readChar()
Answer: b) read()
43. What is the use of the PrintWriter class?
a) To read text files
b) To write formatted output to files
c) To handle exceptions
d) To manage applets
Answer: b) To write formatted output to files
44. Which of the following is a type of JDBC driver?
a) Type-0 Driver
b) Type-1 Driver
c) Type-5 Driver
d) None of the above
Answer: b) Type-1 Driver
45. Which method is used to establish a connection to a database in JDBC?
a) connect()
b) DriverManager.getConnection()
c) establish()
d) init()
Answer: b) DriverManager.getConnection()

General Questions

46. Which keyword is used to declare a constant in Java?


a) final
b) const
c) static
d) constant
Answer: a) final
47. What is the result of the expression 10 / 3 in Java?
a) 3.33
b) 3
c) 4
d) 10
Answer: b) 3
48. Which method in Java is used to find the length of a string?
a) size()
b) count()
c) length()
d) getLength()
Answer: c) length()
49. Which package is automatically imported into every Java program?
a) java.io
b) java.util
c) java.lang
d) java.net
Answer: c) java.lang
50. What does null represent in Java?
a) Zero
b) Undefined value
c) No value or object reference
d) Default value of integers
Answer: c) No value or object reference

You might also like