What are the types of variables in Java? There are eight basic types of variables in Java: byte, short, int, long, float, double, char, and boolean.
int age =25;String name ="John Doe";double salary =5000.50;boolean isEmployed =true;
What are Java packages? Java packages are a mechanism for organizing classes and interfaces. It can group related classes and interfaces together for better code management and maintenance.
packagecom.example.mypackage;publicclassMyClass{
// class implementation}
What is inheritance in Java? Inheritance is a mechanism in Java that allows a class (called a subclass) to inherit the properties and methods of another class (called a parent class).
publicclassAnimal{
String name;int age;publicvoideat(){
System.out.println("Animal is eating");}}publicclassDogextendsAnimal{
String breed;publicvoidbark(){
System.out.println("Dog is barking");}}
What is an interface in Java? An interface is a mechanism in Java that defines a set of methods but provides no implementation. A class can implement one or more interfaces and provide implementations for the methods defined in the interfaces.
What is polymorphism in Java? Polymorphism is a mechanism in Java that allows different objects to respond differently to the same method. This is achieved through inheritance and interfaces.
publicclassAnimal{
publicvoidmakeSound(){
System.out.println("Animal is making a sound");}}publicclassDogextendsAnimal{
publicvoidmakeSound(){
System.out.println("Dog is barking");}}publicclassCatextendsAnimal{
publicvoidmakeSound(){
System.out.println("Cat is meowing");}}Animal myAnimal =newDog();
myAnimal.makeSound();
What is exception handling in Java? Java's exception handling is a mechanism that allows a program to perform some specific actions when an exception occurs, instead of crashing directly.
try{
int result =10/0;}catch(ArithmeticException e){
System.out.println("Exception caught: "+ e.getMessage());}finally{
System.out.println("Finally block executed");}
What are threads in Java? A thread in Java is a mechanism that allows a program to execute multiple tasks simultaneously. Each thread has its own execution path and can run independently, but share the same memory space.
classMyThreadextendsThread{
publicvoidrun(){
System.out.println("Thread is running");}}MyThread t =newMyThread();
t.start();
What are the collections in Java? Collections in Java include data structures such as List, Set, Map, and Queue, which allow storing and manipulating multiple values in a single object.
What is reflection in Java? Reflection in Java is a mechanism that allows programs to obtain class information and manipulate class properties and methods at runtime.
Class<?> c =Class.forName("com.example.MyClass");Object obj = c.newInstance();Method method = c.getMethod("myMethod");
method.invoke(obj);
What are Java annotations? Java annotations are a mechanism that allow programs to add metadata in code. Annotations can be applied to elements such as classes, methods, and variables to provide additional information about these elements.
@MyAnnotation(value ="someValue")publicclassMyClass{
// class implementation}
What are Lambda Expressions in Java? Lambda expressions are a mechanism in Java 8 that allow programs to define anonymous functions in a more concise manner, making the code more readable and maintainable.
List<String> list =newArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
list.forEach((String s)->System.out.println(s));
What are streams in Java? A stream in Java is a mechanism that allows a program to process data in a continuous manner. Streams can read data from files, networks, and other sources and convert it to different formats.
What are generics in Java? Generics in Java are a mechanism that allows a program to specify a generic type parameter at compile time and use that type parameter at run time. This makes the code more flexible and reusable.
List<String> list =newArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");for(String s : list){
System.out.println(s);}
What is an enum in Java? An enumeration in Java is a mechanism that allows a program to group together a group of related constants for better management and maintenance of the code.
What is file IO in Java? File IO in Java is a mechanism that allows programs to read and write files. It allows programs to access files on disk, and to read and write data in files.
What is serialization in Java? Serialization in Java is a mechanism that allows a program to convert an object into a stream of bytes and write it to a file or send it over a network. Deserialization is the process of converting a stream of bytes back into an object.
What are regular expressions in Java? Regular expressions in Java are a mechanism that allow programs to find and replace patterns in text. Regular expressions are strings of special characters that match patterns in text.
What is network programming in Java? Network programming in Java is a mechanism that allows programs to connect to other programs over a network and communicate.
What is an injection attack in Java? An injection attack in Java is a security hole that allows an attacker to add malicious code to a program. Injection attacks can be prevented through input validation and filtering.
What is encryption in Java? Encryption in Java is a mechanism that allows a program to convert data into an unreadable format to protect the security of the data. Encryption can be used for passwords, credit card numbers, and other sensitive information.
What is JDBC in Java? JDBC in Java is a mechanism that allows programs to connect to other programs through a database and interact with data. JDBC is an acronym for Java Database Connectivity.
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost/mydatabase","username","password");Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");while(rs.next()){
// process result set}
rs.close();
stmt.close();
conn.close();
What is JUnit in Java? JUnit in Java is a testing framework that allows programmers to write and run automated tests. JUnit helps programmers find bugs and problems in their code.
importorg.junit.Test;importstaticorg.junit.Assert.*;publicclassMyClassTest{
@TestpublicvoidtestMyMethod(){
MyClass obj =newMyClass();int result = obj.myMethod();assertEquals(10, result);}}
What is logging in Java? Logging in Java is a mechanism that allows the programmer to record the running information of the program and inspect them when needed. Logs can help programmers diagnose and solve problems.
What is JPA in Java? JPA in Java is a mechanism that allows programmers to access databases in an object-oriented manner. JPA is an acronym for Java Persistence API.