Java’s Classes and Objects with a curated set of Multiple Choice Questions (MCQs). These questions delve into the heart of Object-Oriented Programming (OOP) by examining the building blocks of Java applications – classes and objects. Test your knowledge of concepts like instantiation, encapsulation, constructors, methods, and instance variables. Whether you’re a beginner seeking a strong foundation or an experienced programmer aiming to reinforce your expertise, these MCQs offer a comprehensive assessment of your understanding. Get ready to unlock the secrets of Java’s Classes and Objects and elevate your programming prowess.
1.) What is the “this” keyword used in Java?
Answer: Option B
Explanation: The “this” keyword is used to refer to the current instance of the class and is often used to disambiguate between instance variables and method parameters with the same name.
2.) Which of the following is NOT a valid way to create an object in Java?
Answer: Option C
Explanation: Option c is not a valid syntax for creating an object. The correct syntax is MyClass obj = new MyClass();.
3.) What is a class constructor’s purpose in Java?
Answer: Option B
Explanation: A class constructor is used to create an instance (object) of the class and initialize its attributes.
4.) Which keyword is used to prevent a class from being inherited by other classes?
Answer: Option D
Explanation: The “final” keyword is used to indicate that a class cannot be subclassed or inherited by other classes.
5.) What is the relationship between a class and an object in Java?
Answer: Option A
Explanation: An object is a specific instance created from a class’s blueprint, which defines its attributes and behaviors.
6.) What is the default value of an instance variable (non-static field) of an object in Java if not explicitly initialized?
Answer: Option B
Explanation: The default value of an instance variable (object reference) is “null” if not explicitly initialized.
7.) Can a class have multiple constructors in Java?
Answer: Option C
Explanation: A class can have multiple constructors with different parameter lists, enabling different ways to initialize objects.
8.) Which access modifier allows a class member to be accessible within the same package and by subclasses, even if they are in different packages?
Answer: Option B
Explanation: The “protected” access modifier allows class members to be accessible within the same package and by subclasses, regardless of whether they are in the same or different packages.
9.) What is the purpose of the “static” keyword for a class method in Java?
Answer: Option D
Explanation: The “static” keyword is used to define a class-level method that belongs to the class itself, not to any specific instance of the class.
10.) What is the purpose of the “instanceof” operator in Java?
Answer: Option C
Explanation: The “instanceof” operator is used to test whether an object is an instance of a specific class or a class that inherits from it.
Java logical programs list
Java Basic Programs
- Java Hello world program
- Java Program to Add two Numbers
- Java Program to Subtract two Numbers
- Java Program to Multiply two Numbers
- Java Program to Check Whether a Number is Even or Odd.
- Check whether a character is a Vowel or Consonant.
- Java Program to find Average of Two Numbers
- Java Program to find Average of Three Numbers
- Java Program to find the Area of Square
- Java Program to Calculate Simple Interest
- Java Program to Calculate Compound Interest
- List of basic logical programs in Java
- How to print a Fibonacci series up to a given number in Java?
- How to calculate the factorial of a given number in java?
- How to check whether an integer number is a prime number or not in java?
- How to check whether a number is a perfect number or not in java?
- How to check whether a number is an Armstrong number or not in java?
- How to reverse a number in Java?
- How to check whether a number is a Palindrome number or not in Java?
- How to swap two numbers without using a third variable in Java?
- How to reverse a string without using the reverse() method in java?
- How to check whether a String is a Palindrome or not in java?
- How to count characters from the string in java?
- How to check if two strings are anagrams of each other?
- How to count the occurrence of the given character in a string in java?
- How to swap two strings without using the third variable in java?
- Searching Algorithms in Java with Examples
Java String Programs
- String length without using length() method.
- Count the Number of Vowels in a String.
- Swap two strings without using the third variable.
- Reverse a string without using the reverse() method.
- Reverse the words in a given string sentence
- Check whether a String is a Palindrome or not in Java.
- Count characters from the string in Java.
- Count the number of words in the String.
- Check if the two strings are anagrams of each other.
- Count the occurrence of the given character in a string.
- Find the Maximum Occurring Character in a String
- Find the First non-repeating Character in a given String
- Remove all Duplicates from a given String
- Remove a given character from the String
- Find the Sum of Digits in a String.
- Compare two Strings in Java.
- String Permutations Program in Java
- Combinations of String in Java
Java Pattern Printing Programs
Java Array-Based Programs
- Find the Length of an Array
- Get Array Input
- Different Ways to Print an Array
- Print all Negative Elements in an Array.
- Find GCD of N Numbers
- Copy an Array
- Merge Two Arrays.
- Sum of Two Arrays.
- Print Odd/Even No. from an Array.
- Put Even and Odd Elements in 2 Separate Arrays
- Find the Sum of an Array
- Sum of Even Numbers in an Array.
- Average of Odd/Even Numbers in Array.
- Count Even and Odd numbers in Array
- Calculate Average Using Array
- Find the Smallest Number in an Array.
- Find the Largest Number in an Array.
- 2nd Largest Number in an Array.
- Remove duplicate elements in an Array.
- Remove odd numbers from an array Java.
- Add Element to Array in Java.
- Insert Element at Specific Position
- Remove Element From Array in Java
- Count Repeated Elements in an Array
- Compare Two Arrays in Java
- Reverse an Array in Java
- Searching in Java with Examples
Multi-Dimensional Array Programs
- Print Matrix or 2D Array.
- Read a Matrix from the User.
- Add the Two Matrices.
- Subtract the Two Matrices.
- Matrix Multiplication.
- Identity Matrix Program.
- Symmetric Matrix Program.
- Find the Transpose of a Matrix.
- Find the Smallest Element in a Matrix.
- Find the Largest Element in a Matrix.
- Search Matrix in Java.
- The sum of Diagonals of a Matrix.
Java Sorting Programs
Java Conversion Programs
Java Miscellaneous Programs
Java Programs based on the Collection Framework
- ArrayList Manipulation: add, remove Elements, find size
- Write a Java program to reverse a List without using the built-in Collections.reverse() method.
- Find duplicate elements in an ArrayList
- Implement a Java program to sort an ArrayList of integer numbers.
- Implement a Java program to sort an ArrayList of custom objects based on a specific attribute.
- Create a stack using an ArrayList and implement push, pop, and peek operations.
- Convert an ArrayList of strings into a string array.
- Write a Java program to remove all elements from an ArrayList that are greater than a certain value.
- Write a Java program to remove duplicates from an ArrayList while preserving the original order.
- Write a Java program to find the common elements in two given ArrayLists.
- ArrayList partition into two separate lists of odd/even numbers.
- Write a program that extracts a sublist from an ArrayList, given a start and end index.
- Merge two sorted ArrayLists into a single sorted ArrayList.
- Find unique string in ArrayList with its frequency
- Write a program to swap the positions of two elements in an ArrayList given their indices.
- Write a program to Create a Custom LinkedList in Java
- Write a Java program for Basic Queue Implementation using LinkedList
- Write a Java program to create a PriorityQueue to manage a to-do list