0% found this document useful (0 votes)
4 views

Java Interview Question - Kodewala

The document provides an overview of various Java concepts, including access modifiers, ArrayList, classes, constructors, and OOP principles such as inheritance, encapsulation, and polymorphism. It also explains keywords like 'static', 'final', 'this', and 'super', along with exception handling, collections, and data structures like HashMap and HashSet. Additionally, it includes Java programming examples for sorting arrays, reversing strings, and finding duplicates.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Interview Question - Kodewala

The document provides an overview of various Java concepts, including access modifiers, ArrayList, classes, constructors, and OOP principles such as inheritance, encapsulation, and polymorphism. It also explains keywords like 'static', 'final', 'this', and 'super', along with exception handling, collections, and data structures like HashMap and HashSet. Additionally, it includes Java programming examples for sorting arrays, reversing strings, and finding duplicates.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

What are the Access Modifiers in Java? What is Java Arraylist?

1. Access modifiers in Java help to restrict the scope of a 1. The ArrayList class is a resizable array, which can be
class, constructor, variable, method, or data member. found in the java.util package.
2. Types of Access Modifiers: Default, Private, Protected 2. ArrayList<String> cars = new ArrayList<String>();
and Public. 3. Sort an arraylist: collections.sort(arraylist name);

What are the various types of Classes in Java? What is a Java Constructor?

1. Static, Final, Abstract, Concrete and Singleton Class. 1. A constructor in Java is a special method that is used to
initialize objects.
What is Singleton Class in Java? 2. The constructor’s name must match the class name and
1. In object-oriented programming, a singleton class is a class cannot have a return type (like void).
that can have only one object (an instance of the class) at a 3. The constructor is called when the object is created.
time. 4. Types of Constructors: Default and Parameterized.

Java OOPs Concepts: What is Java Super Keyword?

1. Inheritance: it is a mechanism in java by which one class 1. The super keyword refers to superclass (parent) objects.
is allowed to inherit the features (fields and methods) of 2. It is used to call superclass methods, and to access the
another class. superclass constructor.
2. Encapsulation: it is a process of wrapping code and data What is Java this Keyword?
together into a single unit, for example, a capsule which is
mixed of several medicines. The public setXXX() and 1. This keyword refers to the current object in a method or
getXXX() methods are the access points of the instance constructor.
variables of the EncapTest class. 2. The most common use of this keyword is to eliminate the
3. Abstraction: it is the process of hiding certain details and confusion between class attributes and parameters with the
showing only essential information to the users. In java, same name (because a class attribute is shadowed by a
abstraction is achieved by interfaces and abstract method or constructor parameter).
classes.
4. Polymorphism: it means having many forms. Using What is the Final Keyword in Java?
polymorphism, we can perform a single action in different 1. The final keyword in java is used to restrict the user. The
ways. It could be achieved by method overloading and java final keyword can be used in many contexts. The
overriding (Runtime Polymorphism). Final can be:
What is a static keyword in Java? a. Variable: If you make any variable as final, you
cannot change the value of final variable (It will
1. In Java, if we want to access class members, we must first be constant).
create an instance of the class. But there will be situations b. Method: If you make any method as final, you
where we want to access class members without creating cannot override it.
any object. c. Class: If you make any class as final, you cannot
2. In those situations, we can use the static keyword in Java. extend it.
3. The static can be:
a. Variable (also known as a class variable): if we What is Exception Handling in Java?
declare a variable static, all objects of the class 1. The Exception Handling in Java is one of the powerful
share the same static variable. mechanism to handle the runtime errors so that the normal
b. Method (also known as a class method): we can flow of the application can be maintained.
invoke static methods directly using the class 2. Java Exception Keywords:
name. a. Try: The "try" keyword is used to specify a block
c. Block: static blocks are used to initialize the static where we should place an exception code.
variables. b. Catch: The "catch" block is used to handle the
i. // static variable: static int age; exception.
ii. // static block: static {age = 23; } c. finally: The "finally" block is used to execute the
d. Nested class necessary code of the program. It is executed
What is an Array in Java? whether an exception is handled or not.

1. Java array is an object which contains elements of a similar What is the difference between Throw and Throws keywords in
data type. Java?
2. int Array = new int[20];
3. int[][] intArray = new int[10][20];
1. Throw: Java throw keyword is used throw an exception 2. Here, keys are unique identifiers used to associate each
explicitly in the code, inside the function or the block of value on a map.
code. Throw is used within the method. 3. It allows one null for key and multiple nulls for values.
4. It uses put method to insert a new element.
2. Throws: Java throws keyword is used in the method
signature to declare an exception which might be thrown What is Hash Table?
by the function while the execution of the code. Throws is 5. It stores elements in key/value pairs, and it uses put
used with the method signature. method to insert a new element.
1. It does not allow null for key as well as for value.
What is finalize() method in Java?
What is Hash Set?
1. The finalize() method is used just before object is
destroyed and can be called just prior to object creation. 1. It does not allow duplicates.
2. It can have a single null value. It uses add method.
What are Java Wrapper Classes? What is the difference between Integer.parseInt() and
1. Wrapper classes provide a way to use primitive data types Integer.valueOf()?
(int, boolean, etc..) as objects. 1. parseInt(): will be returning the primitive type int.
2. ArrayList<Integer> myNumbers = new 2. valueOf(): will be returning the Integer wrapper Object.
ArrayList<Integer>();
Java Program to “Sort an Array”:
Can We Override Static Method in Java? If not, Why?
1. public class Main {
1. No, we cannot override static methods because methods 2. public static void main(String[] args) {
overriding is based on dynamic binding at runtime and the 3. //define original array
static methods are bonded using static binding at compile 4. int [] intArray = new int []
time. So, we cannot override static methods. {52,45,32,64,12,87,78,98,23,7};
What are collections in Java? 5. int temp = 0;
6. //print original array
1. The Collection in Java is a framework that provides an 7. System.out.println("Original array: ");
architecture to store and manipulate the group of objects. 8. for (int i = 0; i <intArray.length; i++) {
2. Java Collection means a single unit of objects. Java 9. System.out.print(intArray[i] + " ");
Collection framework provides many interfaces (Set, List, 10. }
Queue, Deque) and classes (ArrayList, Vector, LinkedList, 11. //Sort the array in ascending order using two for loops
PriorityQueue, HashSet, LinkedHashSet, TreeSet). 12. for (int i = 0; i <intArray.length; i++) {
13. for (int j = i+1; j <intArray.length; j++) {
What is the difference between Collection and Collections?
14. if(intArray[i] >intArray[j]) { //swap elements if
1. Collection: It is an interface. not in order
2. Collections: It is a utility class. 15. temp = intArray[i];
16. intArray[i] = intArray[j];
What is the difference between Set and List in Java? 17. intArray[j] = temp;
18. }
1. The list implementation allows us to add the same or
19. }
duplicate elements.
20. }
2. The set implementation does not allow us to add the same
21. //print sorted array
or duplicate elements.
22. System.out.println("\nArray sorted in ascending
What is Hashing in Java? order: ");
23. for (int i = 0; i <intArray.length; i++) {
1. Hashing is the process of mapping the data to some 24. System.out.print(intArray[i] + " ");
representative integer value using the concept of hashing 25. }
algorithms. 26. }
2. In Java, a hash code is an integer value that is linked with
each object. Java Program to “Split a String”:
3. Hashing finds its data structure implementation in
1. public class SplitExample{
HashTables and HashMaps.
2. public static void main(String args[]){
What is Hash Map? 3. String s1="java string split method by javatpoint";
4. String[] words=s1.split("\\s");//splits the string based on
1. It stores elements in key/value pairs. whitespace
5. //using java foreach loop to print elements of string array
6. for(String w:words){ 8. for(int i=0;i<size;i++) //Use to hold an element
7. System.out.println(w); 9. {
8. }}} 10. for(int j=i+1;j<size;j++) //Use to check for rest of
the elements
Java Program to “Reverse a String”: 11. {
1. import java.io.*; 12. if(arr[i]<arr[j]) //Compare and swap
2. import java.util.Scanner; 13. {
3. class GFG { 14. int temp=arr[i];
4. public static void main (String[] args) { 15. arr[i]=arr[j];
5. String str= "Geeks", nstr=""; 16. arr[j]=temp;
6. char ch; 17. }}}
7. System.out.print("Original word: "); 18. System.out.println("Largest element is
8. System.out.println("Geeks"); //Example word "+arrayname[size-1]); //Display Largest
9. for (int i=0; i<str.length(); i++) 19. }}
10. { Java program to “Extract a word from a string”:
11. ch= str.charAt(i); //extracts each character
12. nstr= ch+nstr; //adds each character in front of the 1. import java.util.Scanner;
existing string 2. public class string {
13. } 3. public static void main(String args[]){
14. System.out.println("Reversed word: "+ nstr); 4. Scanner sc = new Scanner(System.in);
15. }} 5. String sentence = "This is a bird";
6. System.out.println("Enter a word to extract from the
Java Program to “Reverse any String”: string: ");
1. class ReverseString { 7. String wordToextract = sc.next();
2. public static void main(String[] args) 8. if(sentence.contains(wordToextract)){
3. { 9. int y = sentence.indexOf(wordToextract);
4. String input = "Geeks for Geeks"; 10. System.out.println(y);
5. StringBuilder input1 = new StringBuilder(); 11. String u = sentence.substring(y ,
6. input1.append(input); y+(wordToextract.length()));
7. input1.reverse(); 12. System.out.println(u);
8. System.out.println(input1); }} 13. } }}

What is StringBuilder? Java Program to illustrate a Set and Iterator:

1. StringBuilder in Java is a class used to create a mutable, or 1. import java.util.*;


in other words, a modifiable succession of characters. 2. public class SetDemo {
3. public static void main(String args[])
What is StringBuffer? 4. {
5. Set<String> set = new HashSet<String>();
1. The StringBuffer class is used to create mutable string. It is
6. set.add("Welcome");
same as String class except it is mutable and thread safe.
7. set.add("To");
What is the difference between String, StringBuilder and 8. set.add("Geeks");
StringBuffer? 9. set.add("4");
10. set.add("Geeks");
1. If a string can change and will be accessed from multiple 11. System.out.println("Set: " + set);
threads, use a StringBuffer because StringBuffer is 12. Iterator value = set.iterator();
synchronous, so you have thread-safety. 13. System.out.println("The iterator values are: ");
2. If you don’t want thread-safety than you can also go with 14. while (value.hasNext()) {
StringBuilder class as it is not synchronized. 15. System.out.println(value.next());
16. }}}
Java Program to “Find the Largest and Second Largest
Number in a given Array”: Java program to “Find Duplicate elements in an Array”:
1. public class findElement 1. public class DuplicateElement {
2. { 2. public static void main(String[] args) {
3. public static void main(String []args) 3. //Initialize array
4. { 4. int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
5. int [] arrayname = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3}; 5. System.out.println("Duplicate elements in given
6. int temp, size; array: ");
7. size = arrayname.length;

You might also like