Docsity

MCQs on Java, Quizzes of Computer science

Toggle sidebar
bg1
MCQS ON JAVA:
Inoy01
โ— What is Java?
A. A programming language
B. A database management system
C. An operating system
D. A web browser
Answer: A
โ— What is the correct syntax for declaring a variable in Java?
A. int varName = 5;
B. varName = 5;
C. varName int = 5;
D. int = varName = 5;
Answer: A
โ— What is the output of the following code?
int x = 5;
System.out.println(x++);
A. 4
B. 5
C. 6
D. 7
Answer: B
โ— Which keyword is used to create an object in Java?
A. create
B. new
C. object
D. instance
Answer: B
โ— What is the difference between a class and an object in Java?
A. A class is a blueprint for creating objects, while an object is an instance of a class.
B. A class is a specific type of object, while an object is a general term for any data structure.
C. A class is a method, while an object is a variable.
D. There is no difference.
Answer: A
โ— Which of the following is not a primitive data type in Java?
A. int
B. boolean
pf3
pf4
pf5

Partial preview of the text

Download MCQs on Java and more Quizzes Computer science in PDF only on Docsity!

MCQS ON JAVA:

Inoy โ— What is Java? A. A programming language B. A database management system C. An operating system D. A web browser Answer: A โ— What is the correct syntax for declaring a variable in Java? A. int varName = 5; B. varName = 5; C. varName int = 5; D. int = varName = 5; Answer: A โ— What is the output of the following code? int x = 5; System.out.println(x++); A. 4 B. 5 C. 6 D. 7 Answer: B โ— Which keyword is used to create an object in Java? A. create B. new C. object D. instance Answer: B โ— What is the difference between a class and an object in Java? A. A class is a blueprint for creating objects, while an object is an instance of a class. B. A class is a specific type of object, while an object is a general term for any data structure. C. A class is a method, while an object is a variable. D. There is no difference. Answer: A โ— Which of the following is not a primitive data type in Java? A. int B. boolean

C. double D. string Answer: D โ— What is the default value of an uninitialized boolean variable in Java? A. false B. true C. null D. 0 Answer: A โ— What is the purpose of the final keyword in Java? A. It indicates that a variable's value cannot be changed. B. It indicates that a method cannot be overridden in a subclass. C. It indicates that a class cannot be extended. D. All of the above. Answer: D โ— Which of the following is not a control structure in Java? A. if B. for C. while D. switch Answer: D โ— What is the difference between a while loop and a do-while loop in Java? A. A while loop always executes at least once, while a do-while loop may not execute at all. B. A while loop is a post-test loop, while a do-while loop is a pre-test loop. C. There is no difference. D. A while loop is used for infinite loops, while a do-while loop is used for loops with a known number of iterations. Answer: A โ— What is the purpose of the break keyword in Java? A. It ends the current iteration of a loop. B. It exits a loop entirely. C. It throws an exception. D. It does nothing. Answer: B โ— What is the purpose of the continue keyword in Java? A. It ends the current iteration of a loop. B. It exits a loop entirely. C. It skips the rest of the code in a loop and moves on to the next iteration.

A. It indicates that a class cannot be extended. B. It indicates that a class must be extended. C. It defines a contract for a class to implement a set of methods. D. It does nothing. Answer: C โ— Which of the following is not a type of exception in Java? A. ArithmeticException B. NullPointerException C. IOException D. ForLoopException Answer: D โ— What is the purpose of the try-catch block in Java? A. To catch and handle exceptions that may occur during the execution of code. B. To create a new object. C. To declare a variable. D. To define a loop. Answer: A โ— What is the purpose of the finally block in a try-catch-finally block in Java? A. To execute code that must be run regardless of whether an exception occurs or not. B. To catch exceptions that may occur during the execution of code. C. To declare a variable. D. To define a loop. Answer: A โ— What is the purpose of the throw keyword in Java? A. To explicitly throw an exception. B. To catch an exception. C. To declare a variable. D. To define a loop. Answer: A โ— Which method is called when an object is created in Java? A. init() B. main() C. constructor() D. finalize() Answer: C โ— What is the purpose of the toString() method in Java? A. To convert an object to a string. B. To convert a string to an object.

C. To initialize an object. D. To define a loop. Answer: A โ— Which method is called when an object is garbage collected in Java? A. init() B. main() C. constructor() D. finalize() Answer: D โ— What is the purpose of the package keyword in Java? A. To indicate the name of the current package. B. To indicate the name of the current class. C. To import a package. D. To define a package. Answer: D