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

Core Java 5 Line Interview Answers

The document provides a comprehensive overview of Java programming concepts, including how Java works, basic syntax, data types, control statements, object-oriented principles, and advanced topics like multithreading and exception handling. It emphasizes key features such as portability, encapsulation, inheritance, and the use of interfaces. Additionally, it offers practical tips for coding and preparation for job placements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Core Java 5 Line Interview Answers

The document provides a comprehensive overview of Java programming concepts, including how Java works, basic syntax, data types, control statements, object-oriented principles, and advanced topics like multithreading and exception handling. It emphasizes key features such as portability, encapsulation, inheritance, and the use of interfaces. Additionally, it offers practical tips for coding and preparation for job placements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) How Java Works

• Java follows the principle of “Write Once, Run Anywhere”.


• The Java compiler converts source code into bytecode.
• This bytecode is platform independent.
• The JVM executes the bytecode on different operating systems.
• This makes Java portable and secure.
2) Hello World Program
• It is the first and simplest Java program.
• The main() method is the entry point of execution.
• public makes the class accessible everywhere.
• static allows JVM to call main without creating an object.
• [Link]() prints output to the console.
3) Keywords
• Keywords are predefined reserved words in Java.
• They have special meaning to the compiler.
• They cannot be used as variable or method names.
• Examples include class, public, static, and final.
• Java has more than 50 reserved keywords.
4) Data Types and Variables
• Data types define the type of data a variable can store.
• Java has primitive and non-primitive data types.
• Primitive types include int, float, double, char, boolean.
• Non-primitive types include String, arrays, and objects.
• Variables are used to store values in memory.
5) Rules for Naming Identifiers
• Identifiers must start with a letter, $ or _.
• They cannot start with a number.
• They cannot be Java reserved keywords.
• Identifiers are case-sensitive.
• Example: age and Age are different.
6) Output Methods
• [Link]() prints output without new line.
• [Link]() moves cursor to next line.
• [Link]() is used for formatted output.
• These methods are part of PrintStream class.
• They help display results to the user.
7) Operators in Java
• Operators perform operations on variables.
• Arithmetic operators include +, -, *, /, %.
• Relational operators compare values.
• Logical operators include &&, ||, !.
• Assignment operators assign values to variables.
8) Pre vs Post Increment
• Pre-increment is written as ++a.
• In pre-increment, value increases first.
• Then the updated value is used.
• Post-increment is written as a++.
• In post-increment, value is used first, then increased.
9) Control Statements
• Control statements manage program flow.
• They include decision-making statements.
• They also include looping statements.
• Jumping statements like break and continue are included.
• They make programs logical and dynamic.

10) Conditional Statements


• Conditional statements help in decision-making.
• if executes code when condition is true.
• else executes code when condition is false.
• else if checks multiple conditions.
• They make programs smarter and flexible.
11) else-if vs switch
• if-else works with boolean conditions.
• It can check complex logical expressions.
• switch works with fixed values.
• It is faster for multiple choices of one variable.
• Both are used based on requirement.
12) Loops
• Loops are used for repetition.
• for is used when iterations are known.
• while runs based on condition.
• do-while executes at least once.
• Loops reduce code repetition.
13) break and continue
• break terminates the loop.
• It stops execution immediately.
• continue skips current iteration.
• It moves to next iteration of loop.
• Both control loop behavior.
14) Command Line Arguments
• These are inputs given during program execution.
• They are stored in String[] args.
• They are passed through main method.
• They allow dynamic input without Scanner.
• Useful in real-time applications.
15) Class and Object
• Class is a blueprint for objects.
• It contains variables and methods.
• Object is an instance of a class.
• Objects occupy memory.
• Multiple objects can be created from one class.
16) Scanner Class
• Scanner is used for user input.
• It belongs to [Link] package.
• It provides methods like nextInt().
• It can read different data types.
• It is easy to use but slower than BufferedReader.
17) Arrays
• Array stores multiple values.
• All elements must be same type.
• It has fixed size.
• Index starts from 0.
• It provides fast access to elements.
18) Two-Dimensional Array
• It is an array of arrays.
• Used to represent matrices.
• It has rows and columns.
• Declared as int[][] arr.
• Used in real-life applications like grids.
19) Matrix Operations
• Java supports matrix addition.
• Also supports matrix multiplication.
• Transpose can be calculated using loops.
• These require nested loops.
• Commonly asked in coding interviews.
20) Enhanced for Loop
• Also called for-each loop.
• Used to traverse arrays.
• It simplifies code.
• No need of index.
• Improves readability.
21) Jagged Array
• Jagged array has different row sizes.
• Each row can have different length.
• It is not rectangular.
• Declared as int[][] arr.
• Useful in memory optimization.
22) Constructors
• Constructor initializes objects.
• It has same name as class.
• It has no return type.
• It is called automatically.
• Used to assign initial values.
23) Constructor Overloading
• Multiple constructors in a class.
• They have different parameters.
• Helps in flexibility.
• Improves code readability.
• Allows different ways to create objects.
24) Encapsulation
• It wraps data and methods together.
• Variables are made private.
• Access provided through getters and setters.
• Increases data security.
• Improves maintainability.
25) Method Overloading
• Same method name, different parameters.
• Also called compile-time polymorphism.
• Improves readability.
• Reduces code duplication.
• Example: add(int), add(int,int).
26) Varargs
• Allows variable number of arguments.
• Represented by ...
• Makes methods flexible.
• Used when number of inputs is unknown.
• Example: sum(int... a).

27) Inheritance
• One class acquires properties of another.
• Parent class is called superclass.
• Child class is called subclass.
• Supports code reusability.
• Implemented using extends.
28) Method Overriding
• Child class redefines parent method.
• Method signature must be same.
• Also called runtime polymorphism.
• Uses @Override annotation.
• Supports dynamic binding.
29) this Keyword
• Refers to current object.
• Used to avoid name conflict.
• Differentiates local and instance variables.
• Can call current class constructor.
• Improves clarity.
30) final Keyword
• Final variable becomes constant.
• Final method cannot be overridden.
• Final class cannot be inherited.
• Improves security.
• Prevents modification.
31) super Keyword
• Refers to parent class object.
• Used to call parent constructor.
• Can call parent methods.
• Used in inheritance.
• Helps in method overriding.
32) static Keyword
• Belongs to class, not object.
• Can be accessed without object.
• Used for memory efficiency.
• Commonly used in main method.
• Shared among all objects.
33) Methods
• Methods perform specific tasks.
• They improve code reusability.
• Reduce redundancy.
• Make program modular.
• Can return values.
34) Abstraction
• Hides implementation details.
• Shows only essential features.
• Achieved using abstract class.
• Also achieved using interface.
• Improves security.
35) Inner Class
• A class inside another class.
• Used for better encapsulation.
• Can access outer class members.
• Reduces code complexity.
• Used in event handling.
36) Why Multiple Inheritance Not Supported?
• To avoid diamond problem.
• It causes ambiguity.
• Java avoids this using interfaces.
• Interfaces allow multiple inheritance.
• Ensures clarity and safety.
37) Interface
• Blueprint of a class.
• Contains abstract methods.
• Used for abstraction.
• Supports multiple inheritance.
• Implemented using implements.
38) Multiple Inheritance using Interface
• Java allows multiple interfaces.
• A class can implement many interfaces.
• Avoids ambiguity.
• Provides flexibility.
• Common in real applications.
39) Functional Interface
• Has only one abstract method.
• Used with lambda expressions.
• Example: Runnable, Comparator.
• Annotated with @FunctionalInterface.
• Improves functional programming.
40) Marker Interface
• It has no methods.
• Used to provide special behavior.
• Example: Serializable.
• Acts as a signal to JVM.
• Used in frameworks.
41) Default and Static Methods in Interface
• Default methods have body.
• They allow backward compatibility.
• Static methods belong to interface.
• They cannot be overridden.
• Called using interface name.
42) Enum
• Enum represents fixed constants.
• Example: DAYS, COLORS.
• Improves type safety.
• More readable than integers.
• Can have methods too.
43) Exception Handling
• Handles runtime errors.
• Uses try, catch, finally.
• throw is used to throw exception.
• throws is used in method signature.
• Prevents program crash.

44) BufferedReader
• Faster than Scanner.
• Reads input efficiently.
• Uses InputStreamReader.
• Suitable for large data.
• Requires exception handling.
45) Multithreading (Thread Class)
• Allows multiple tasks simultaneously.
• Created by extending Thread class.
• Uses start() method.
• Improves performance.
• Uses separate stack memory.
46) Multithreading (Runnable Interface)
• Preferred over Thread class.
• Avoids multiple inheritance issue.
• Implements run() method.
• More flexible design.
• Used in real projects.
47) Thread Methods
• start() begins execution.
• sleep() pauses thread.
• join() waits for another thread.
• yield() gives chance to others.
• interrupt() stops thread.
48) Thread Synchronization
• Prevents data inconsistency.
• Allows one thread at a time.
• Uses synchronized keyword.
• Protects shared resources.
• Important in multithreading.
49) String
• String is immutable.
• Cannot be changed after creation.
• Stored in String pool.
• Used frequently in Java.
• Represents sequence of characters.
50) String Methods
• length() returns size.
• toUpperCase() converts to uppercase.
• substring() extracts part of string.
• equals() compares values.
• trim() removes spaces.
51) StringBuffer vs StringBuilder
• StringBuffer is thread-safe.
• StringBuilder is not thread-safe.
• StringBuilder is faster.
• StringBuffer is slower but safe.
• Used for mutable strings.
52) Wrapper Class
• Converts primitive to object.
• Example: int to Integer.
• Supports autoboxing.
• Supports unboxing.
• Used in collections.
53) Collections Framework
• Provides data structures.
• Includes List, Set, Map.
• Helps store objects.
• Improves performance.
• Part of [Link].
54) List Interface
• Allows duplicate elements.
• Maintains insertion order.
• Examples: ArrayList, LinkedList.
• Index-based access.
• Widely used.
55) Set Interface
• Does not allow duplicates.
• No guaranteed order (HashSet).
• Example: HashSet, TreeSet.
• Used for unique elements.
• Faster lookup.
56) Map Interface
• Stores key-value pairs.
• Keys must be unique.
• Examples: HashMap, TreeMap.
• Used in real applications.
• Fast retrieval.
57) Packages
• Groups related classes.
• Avoids name conflicts.
• Improves organization.
• Provides access control.
• Example: [Link].
58) Access Modifiers
• Control visibility of members.
• Types: public, private, protected, default.
• private is most restrictive.
• public is least restrictive.
• Improves security.
59) Date Time API
• Introduced in Java 8.
• Includes LocalDate, LocalTime.
• More reliable than old Date class.
• Thread-safe.
• Used in modern applications.
60) File Handling
• File class is used for files.
• Can create, read, write, delete files.
• Part of [Link] package.
• Supports file operations.
• Used in real projects.

61) Placement Tips


• Be confident and calm.
• Know your basics clearly.
• Practice coding daily.
• Be honest in answers.
• Show willingness to learn.

You might also like