My Practice
  • Introduction
  • Chapter 1 Combination and Permutation
    • Summary 1: Template
    • Subsets
      • Problem 78: Subsets
      • Problem 90: Unique Subsets
    • Permutation
      • Problem 46: Permutations
      • Problem 47: Unique Permutations
      • Problem 31: Next Permutation
      • Problem: Previous Permutation
      • Problem 60: Permutation Sequence
    • Combination
      • Problem 39: Combination Sum
      • Problem 40: Combination Sum II
      • Problem 216: Combination Sum III
      • Problem 377: Combination Sum IV
      • Problem 77: Combinations
      • Problem 491: Increasing Subsequences
    • Binary Tree Path Sum
      • Problem 112: Path Sum
      • Problem 113: Path Sum II
      • Problem 437: Path Sum III
    • Problem 22: Generate Parentheses
    • Problem 51: N-Queens
      • Problem 52: N-Queens II
      • Problem 37: Sudoku Solver
    • Problem 17: Letter Combinations of a Phone Number
    • Problem 93: Restore IP Addresses
    • Problem 131: Palindrome Partitioning
    • Problem 49: Group Anagrams
    • Problem 401: Binary Watch
    • Problem 320: Generalized Abbreviation
  • Chapter 2 Binary Search & Sorted Array
    • Summary 1: Binary Search Template
    • Pow & Sqrt
      • Problem 50: Pow(x, n)
      • Problem 69: Sqrt(x)
      • Problem 231: Power of Two
      • Problem: Fast Power (LintCode)
      • Problem 372: Super Pow
      • Problem 172: Factorial Trailing Zeroes
    • Problem 34: Search for a Range
    • Problem 35: Search Insert Position
    • Problem 74: Search a 2D Matrix
    • Problem 240: Search a 2D Matrix II
    • Problem 33: Search in Rotated Sorted Array
    • Problem 81: Search in Rotated Sorted Array II
    • Problem 278: First Bad Version
    • Problem 162: Find Peak Element
    • Sorted Array
      • Problem: Partition Array (LintCode)
      • Problem 26: Remove Duplicates from Sorted Array
      • Problem 80: Remove Duplicates from Sorted Array II
      • Problem 88: Merge Sorted Array
      • Problem: Merge Two Sorted Arrays II (LintCode)
      • Problem: Recover Rotated Sorted Array (LintCode)
      • Problem: Rotate String (LintCode)
      • Problem 4: Median of Two Sorted Arrays
    • Problem 215: Kth Largest Element in an Array
    • Problem 29: Divide Two Integers
    • Problem 287: Find the Duplicate Number
    • Problem 374: Guess Number Higher or Lower
  • Chapter 3 Binary Tree
    • Basics
      • Problem 110: Balanced Binary Tree
      • Problem 98: Validate Binary Search Tree
        • Problem 230: Kth Smallest Element in a BST
        • Problem: Convert Binary Search Tree to Doubly Linked List (LintCode)
      • Problem 104: Maximum Depth of Binary Tree
      • Problem 111: Minimum Depth of Binary Tree
      • Problem 100: Same Tree
        • Problem 101: Symmetric Tree
      • Problem 450: Delete Node in a BST
    • Traversals
      • Problem 144: Binary Tree Preorder Traversal
      • Problem 94: Binary Tree Inorder Traversal
        • Problem 99: Recover Binary Search Tree
      • Problem 145: Binary Tree Postorder Traversal
      • Problem 102: Binary Tree Level Order Traversal
        • Problem 107: Binary Tree Level Order Traversal II
        • Problem 103: Binary Tree Zigzag Level Order Traversal
        • Problem 515: Find Largest Value in Each Tree Row
      • Problem 314: Binary Tree Vertical Order Traversal
      • Problem 105: Construct Binary Tree from Preorder and Inorder Traversal
      • Problem 106: Construct Binary Tree from Inorder and Postorder Traversal
      • Problem: Construct BST from given preorder traversal
      • Problem 285: Inorder Successor in BST
      • Problem 255: Verify Preorder Sequence in Binary Search Tree
    • Ancestors
      • Problem 236: Lowest Common Ancestor of a Binary Tree
      • Problem 235: Lowest Common Ancestor of a Binary Search Tree
    • Serialize and Deserialize
      • Problem 297: Serialize and Deserialize Binary Tree
      • Problem 449: Serialize and Deserialize BST
    • LinkedList 与 Binary Tree 相互转换
      • Problem 109: Convert Sorted List to Binary Search Tree
      • Problem 108: Convert Sorted Array to Binary Search Tree
      • Problem 116: Populating Next Right Pointers in Each Node
        • Problem 117: Populating Next Right Pointers in Each Node II
      • Problem 114: Flatten Binary Tree to Linked List
    • Problem: Insert Node in a Binary Search Tree (LintCode)
    • Problem: Search Range in Binary Search Tree (LintCode)
    • Problem 257: Binary Tree Paths
    • Problem 124: Binary Tree Maximum Path Sum
    • Problem 129: Sum Root to Leaf Numbers
    • Problem 173: Binary Search Tree Iterator
    • Problem 298: Binary Tree Longest Consecutive Sequence
    • Problem 199: Binary Tree Right Side View
    • Problem 250: Count Univalue Subtrees
    • Problem 96: Unique Binary Search Trees
    • Problem 95: Unique Binary Search Trees II
    • Problem 156: Binary Tree Upside Down
    • Problem 366: Find Leaves of Binary Tree
    • Problem 404: Sum of Left Leaves
    • Problem 270: Closest Binary Search Tree Value
      • Problem 272: Closest Binary Search Tree Value II
    • Problem 333: Largest BST Subtree
    • Problem 315: Count of Smaller Numbers After Self
  • Chapter 4 DFS & BFS
    • Summary 1: DFS Template
    • DFS
      • Problem 339: Nested List Weight Sum
        • Problem 364: Nested List Weight Sum II
    • Topological Sort
      • Problem 207: Course Schedule
    • Problem 133: Clone Graph
    • Problem 138: Copy List with Random Pointer
    • Problem 301: Remove Invalid Parentheses
    • Print Char Board
    • Problem 200: Number of Islands
      • Problem 305: Number of Islands II
      • Problem 463: Island Perimeter
    • Problem 127: Word Ladder
      • Problem 126: Word Ladder II
    • Topological Sort
    • Problem 332: Reconstruct Itinerary
    • Problem 290: Word Pattern
      • Problem 291: Word Pattern II
    • Problem 79: Word Search
  • Chapter 5 Linked List
    • Summary 1: Pointer and update
    • Reverse Linked List
      • Problem 206: Reverse Linked List
      • Print reverse order of a Linked List
      • Problem 92: Reverse Linked List II
      • Problem 61: Rotate List
      • Problem 143: Reorder List
      • Problem 24: Swap Nodes in Pairs
      • Problem 86: Partition List
    • Iteration
      • Problem 83: Remove Duplicates from Sorted List
      • Problem 82: Remove Duplicates from Sorted List II
      • Problem 328: Odd Even Linked List
    • Basic Operations
      • Problem 148: Sort List
      • Problem 19: Remove Nth Node From End of List
        • Problem 203: Remove Linked List Elements
      • Problem 21: Merge Two Sorted Lists
      • Problem 23: Merge k Sorted Lists
        • Problem: Merge K sorted Arrays
      • Problem 237: Delete Node in a Linked List
      • Problem 147: Insertion Sort List
    • Find Cycles
      • Problem 141: Linked List Cycle
      • Problem 142: Linked List Cycle II
    • Problem 234: Palindrome Linked List
  • Chapter 6 Dynamic Programming
    • Matrix 类型
      • Problem120: Triangle
      • Problem 62: Unique Paths
      • Problem 63: Unique Paths II
      • Problem 64: Minimum Path Sum
      • Problem 221: Maximal Square
    • Sequence 类型
      • Problem 70: Climbing Stairs
      • Problem 91: Decode Ways
      • Problem 55: Jump Game
      • Problem 139: Word Break
        • Problem 140: Word Break II
      • Problem 132: Palindrome Partitioning II
      • Problem 300: Longest Increasing Subsequence
    • Two Sequences 类型
      • Problem: Longest Common Subsequence (LintCode)
      • Problem 72: Edit Distance
    • Backpack
      • Problem: Backpack I
      • Problem: Backpack II
      • Problem: Backpack III
    • Optimal Solution
      • Problem 322: Coin Change
    • Problem 198: House Robber
      • Problem 213: House Robber II
      • Problem 337: House Robber III
    • Problem 361: Bomb Enemy
      • Calculate Crossing
    • Problem 279: Perfect Squares
    • Problem 418: Sentence Screen Fitting
    • Optimal Solution
  • Chapter 7 Graph & Search
  • Chapter 8 Data Structure
    • Stack & Queue
      • Problem 232: Implement Queue using Stacks
      • Problem 155: Min Stack
      • Problem 84: Largest Rectangle in Histogram
        • Problem 85: Maximal Rectangle
      • Problem: Construct Max Tree
      • Problem 20: Valid Parentheses
        • Problem 394: Decode String
        • Problem 439: Ternary Expression Parser
        • Problem 241: Different Ways to Add Parentheses
        • Problem 282: Expression Add Operators
    • Hash
      • Problem 146: LRU Cache
      • Problem 128: Longest Consecutive Sequence
    • Heap
      • Problem 295: Find Median from Data Stream
      • Problem 218: The Skyline Problem
    • Trie
      • Problem 208: Implement Trie (Prefix Tree)
      • Problem 211: Add and Search Word - Data structure design
      • Problem 425: Word Squares
    • Problem 341: Flatten Nested List Iterator
    • Problem 281: Zigzag Iterator
    • Problem 346: Moving Average from Data Stream
    • Problem 380: Insert Delete GetRandom O(1)
      • Problem 381: Insert Delete GetRandom O(1) - Duplicates allowed
  • Chapter 9 High Frequency
    • Single Number
      • Problem 136: Single Number
      • Problem 137: Single Number II
      • Problem 260: Single Number III
      • Problem 169: Majority Element
      • Problem 229: Majority Element II
      • Problem: Majority Number III (LintCode)
    • Buying and Selling Stock
      • Problem 121: Best Time to Buy and Sell Stock
      • Problem 122: Best Time to Buy and Sell Stock II
      • Problem 123: Best Time to Buy and Sell Stock III
      • Problem 188: Best Time to Buy and Sell Stock IV
      • Problem 309: Best Time to Buy and Sell Stock with Cooldown
    • Subarray Sum
      • Problem 53: Maximum Subarray
      • Problem: Maximum Subarray II (LintCode)
      • Problem: Maximum Subarray Difference (LintCode)
      • Problem: Maximum Subarray III (LintCode)
      • Problem: Minimum Subarray (LintCode)
      • Problem: Subarray Sum (LintCode)
      • Problem: Subarray Sum Closest (LintCode)
      • Problem 152: Maximum Product Subarray
    • Two Sum
      • Problem 1: Two Sum
      • Problem 167: Two Sum II - Input array is sorted
      • Problem 15: 3Sum
      • Problem 259: 3Sum Smaller
      • Problem 16: 3Sum Closest
      • Problem 18: 4Sum
      • Problem: k Sum (LintCode)
      • Problem 416: Partition Equal Subset Sum
        • Problem 494: Target Sum
        • Problem 473: Matchsticks to Square
    • Problem: Sort Letters by Case (LintCode)
    • Problem 75: Sort Colors
  • Post Chapter 1 String
    • Sliding Window Problems
      • Problem 438: Find All Anagrams in a String
      • Problem 76: Minimum Window Substring
      • Problem 159: Longest Substring with At Most Two Distinct Characters
      • Problem 3: Longest Substring Without Repeating Characters
      • Problem 340: Longest Substring with At Most K Distinct Characters
      • Problem 239: Sliding Window Maximum
      • Problem 187: Repeated DNA Sequences
    • Problem 345: Reverse Vowels of a String
    • Problem 344: Reverse String
    • Problem 165: Compare Version Numbers
    • Problem151: Reverse Words in a String
    • Problem 125: Valid Palindrome
    • Problem 12: Integer to Roman
    • Problem 28: Implement strStr()
    • Problem 65: Valid Number
    • Problem 38: Count and Say
    • Problem 68: Text Justification
    • Problem 58: Length of Last Word
    • Problem 5: Longest Palindromic Substring
    • Problem 71: Simplify Path
    • Problem 227: Basic Calculator II
    • Problem 383: Ransom Note
    • Problem 8: String to Integer (atoi)
    • Problem 388: Longest Absolute File Path
    • Problem 288: Unique Word Abbreviation
    • Problem 161: One Edit Distance
    • Problem 271: Encode and Decode Strings
    • Problem 299: Bulls and Cows
    • Problem 44: Wildcard Matching
    • Problem 336: Palindrome Pairs
    • Problem 179: Largest Number
    • Problem 482: License Key Formatting
  • Post Chapter 2 Math
    • Bit Manipulation
      • Problem 371: Sum of Two Integers
      • Problem 461: Hamming Distance
      • Problem 191: Number of 1 Bits
      • Problem 421: Maximum XOR of Two Numbers in an Array
      • Problem 190: Reverse Bits
      • Problem 318: Maximum Product of Word Lengths
      • Problem 751: IP to CIDR
    • Problem 7: Reverse Integer
    • Problem 66: Plus One
    • Problem 9: Palindrome Number
    • Problem 2: Add Two Numbers
    • Problem 389: Find the Difference
    • Problem 13: Roman to Integer
    • Problem 273: Integer to English Words
    • Problem 67: Add Binary
      • Problem 258: Add Digits
    • Problem 43: Multiply Strings
    • Problem 268: Missing Number
    • Problem 343: Integer Break
    • Problem 357: Count Numbers with Unique Digits
    • Problem 201: Bitwise AND of Numbers Range
    • Problem 342: Power of Four
      • Problem 326: Power of Three
    • Problem 168. Excel Sheet Column Title
      • Problem 171: Excel Sheet Column Number
    • Problem 202: Happy Number
    • line
      • Problem 149: Max Points on a Line
    • Problem 319: Bulb Switcher
    • Problem 223: Rectangle Area
    • Problem 263: Ugly Number
  • Post Chapter 3 Array
    • Problem 56: Merge Intervals
    • Problem 57: Insert Interval
    • Problem 11: Container With Most Water
      • Problem 42: Trapping Rain Water
    • Problem 14: Longest Common Prefix
    • Problem 36: Valid Sudoku
    • Problem 209: Minimum Size Subarray Sum
    • Problem 73: Set Matrix Zeroes
    • Problem 48: Rotate Image
    • Problem 54: Spiral Matrix
    • Problem 59: Spiral Matrix II
    • Problem 118: Pascal's Triangle
    • Problem 119: Pascal's Triangle II
    • Problem 189: Rotate Array
    • Problem 217: Contains Duplicate
    • Problem 219: Contains Duplicate II
    • Problem 220: Contains Duplicate III
    • Problem 311: Sparse Matrix Multiplication
      • Problem: Sparse vectors of dot product
    • Reservoir Sampling
      • Problem 398: Random Pick Index
        • Random Non-Empty Slots
      • Problem 384: Shuffle an Array
    • Problem 228: Summary Ranges
    • Problem 238: Product of Array Except Self
    • Problem 283: Move Zeroes
    • Problem 289: Game of Life
    • Problem 396: Rotate Function
    • Problem 242: Valid Anagram
    • Problem 349: Intersection of Two Arrays
      • Problem 160: Intersection of Two Linked Lists
    • Problem 350: Intersection of Two Arrays II
    • Problem 387: First Unique Character in a String
    • Problem 334: Increasing Triplet Subsequence
    • Problem 325: Maximum Size Subarray Sum Equals k
    • Problem 252: Meeting Rooms
    • Problem 253: Meeting Rooms II
    • Problem 277: Find the Celebrity
    • Problem 157: Read N Characters Given Read4
    • Problem 158: Read N Characters Given Read4 II - Call multiple times
    • Problem 163: Missing Ranges
    • Problem 360: Sort Transformed Array
    • Problem 280: Wiggle Sort
      • Problem 324: Wiggle Sort II
    • Problem 274: H-Index
    • Problem 448: Find All Numbers Disappeared in an Array
      • Problem 442: Find All Duplicates in an Array
  • Facebook
    • K closest points
    • Find all the peak and valley
    • Arithmetic operation combination
    • Task Schedule
    • Fibonacci numbers
    • LinkedList BlackBox
  • Amazon
    • Right Rotation
    • Consecutive Gray Code
    • Valid Parentheses
    • Reverse Second Half of Linked List
    • Subtree
    • Find K Nearest Point
    • Overlap Rectangle
    • Window Sum
    • GCD Greatest Common Divisor
  • All Chapters Summary
    • Two Pointers
    • Permutation and Combination
    • Basic Data Structures
    • Binary search and Divide and Conquer
    • Binary Tree
    • Linked List
    • Tree
    • Bit Manipulation
  • Core Java Interview Questions
    • Sec 1: Basics of Java Questions
    • Sec 2: OOP Concepts
    • Sec 3: Exception Handling
    • Sec 4: String Handling
    • Sec 5: Nested Classes and Interfaces
    • Sec 6: Garbage Collection and I\/O Questions
    • Sec 7: Serialization, Networking and Reflection Questions
    • Sec 8: Miscellaneous Questions
    • Sec 9: Multithreading Questions
    • Sec 10: Java Collection Questions
  • Basics of Sorting
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Behavior Questions
    • Introduction to yourself
    • Why change Physics to CS
    • Biggest problems in working with others
    • Project 1: Apartment Finder Website
    • Project 2: Mini UNIX Shell
    • Project 3: 911 Call Data Mining
    • Project 4: Andriod Weather Forecast App
Powered by GitBook
On this page
  • 13) What is difference between object oriented programming language and object based programming language?
  • 14) What will be the initial value of an object reference which is defined as an instance variable?
  • 15) What is constructor?
  • 16) What is the purpose of default constructor?
  • 17) Does constructor return any value?
  • 18) Is constructor inherited?
  • 19) Can you make a constructor final?
  • 20) What is static variable?
  • 21) What is static method?
  • 22) Why main method is static?
  • 24) Can we execute a program without main() method?
  • 25) What if the static modifier is removed from the signature of the main method?
  • 26) What is difference between static (class) method and instance method?
  • 27) What is this in java?
  • 28)What is Inheritance?
  • 29) Which class is the superclass for every class.
  • 30) Why multiple inheritance is not supported in java?
  • 31) What is composition?
  • 32) What is difference between aggregation and composition?
  • 33) Why Java does not support pointers?
  • 34) What is super in java?
  • 35) Can you use this() and super() both in a constructor?
  • 36)What is object cloning?
  • 37) What is method overloading?
  • 38) Why method overloading is not possible by changing the return type in java?
  • 39) Can we overload main() method?
  • 40) What is method overriding?
  • 41) Can we override static method?
  • 42) Why we cannot override static method?
  • 43) Can we override the overloaded method?
  • 44) Difference between method Overloading and Overriding.
  • 举例说明 overriding Vs. overloading
  • 45) Can you have virtual functions in Java?
  • 46) What is covariant return type?
  • 47) What is final variable?
  • 48) What is final method?
  • 49) What is final class?
  • 50) What is blank final variable?
  • 51) Can we intialize blank final variable?
  • 52) Can you declare the main method as final?
  • 53) What is Runtime Polymorphism?
  • 54) Can you achieve Runtime Polymorphism by data members?
  • 举例说明多态
  • 55) What is the difference between static binding and dynamic binding?
  • 56) What is abstraction?
  • 57) What is the difference between abstraction and encapsulation?
  • 58) What is abstract class?
  • 59) Can there be any abstract method without abstract class?
  • 60) Can you use abstract and final both with a method?
  • 61) Is it possible to instantiate the abstract class?
  • 62) What is interface?
  • 63) Can you declare an interface method static?
  • 64) Can an Interface be final?
  • 65) What is marker interface?
  • 66) What is difference between abstract class and interface?
  • 67) Can we define private and protected modifiers for variables in interfaces?
  • 68) When can an object reference be cast to an interface reference?
  • 69) What is package?
  • 70) Do I need to import java.lang package any time? Why ?
  • 71) Can I import same package/class twice? Will the JVM load the package twice at runtime?
  • 72) What is static import ?

Was this helpful?

  1. Core Java Interview Questions

Sec 2: OOP Concepts

13) What is difference between object oriented programming language and object based programming language?

Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.

14) What will be the initial value of an object reference which is defined as an instance variable?

The object references are all initialized to null in Java.

15) What is constructor?

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

16) What is the purpose of default constructor?

The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

17) Does constructor return any value?

yes, that is current instance (You cannot use return type yet it returns a value).

18) Is constructor inherited?

No, constructor is not inherited.

19) Can you make a constructor final?

No, constructor can't be final.

20) What is static variable?

  • static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.

  • static variable gets memory only once in class area at the time of class loading.

    (1) Program of counter without static variable

21) What is static method?

  • A static method belongs to the class rather than object of a class.

  • A static method can be invoked without the need for creating an instance of a class.

  • static method can access static data member and can change the value of it.

22) Why main method is static?

Because object is not required to call static method if It were non-static method, jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

24) Can we execute a program without main() method?

Yes, one of the way is static block.

25) What if the static modifier is removed from the signature of the main method?

Program compiles. But at runtime throws an error "NoSuchMethodError".

26) What is difference between static (class) method and instance method?

27) What is this in java?

It is a keyword that that refers to the current object. (1) the problem without this keyword

(2) Solution of the above problem by this keyword

28)What is Inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

29) Which class is the superclass for every class.

Object class.

30) Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.

31) What is composition?

Holding the reference of the other class within some other class is known as composition.

32) What is difference between aggregation and composition?

Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).

33) Why Java does not support pointers?

Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

34) What is super in java?

It is a keyword that refers to the immediate parent class object.

35) Can you use this() and super() both in a constructor?

No. Because super() or this() must be the first statement (Constructor must always be the first statement).

36)What is object cloning?

The object cloning is used to create the exact copy of an object.

37) What is method overloading?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.

38) Why method overloading is not possible by changing the return type in java?

Because of ambiguity.

39) Can we overload main() method?

40) What is method overriding?

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

41) Can we override static method?

No, you can't override the static method because they are the part of class not object.

42) Why we cannot override static method?

It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

43) Can we override the overloaded method?

Yes.

44) Difference between method Overloading and Overriding.

举例说明 overriding Vs. overloading

class Vehicle {
    void run() {
        System.out.println("Vihicle can run.");
    }
}

class Bike extends Vehicle {
    void run() {
        System.out.println("Bike can run at 20 miles / hour");
    }
}

45) Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default.

46) What is covariant return type?

Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.

47) What is final variable?

If you make any variable as final, you cannot change the value of final variable (It will be constant).

48) What is final method?

Final methods can't be overriden.

49) What is final class?

Final class can't be inherited.

50) What is blank final variable?

A final variable, not initalized at the time of declaration, is known as blank final variable.

51) Can we intialize blank final variable?

Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.

52) Can you declare the main method as final?

Yes, such as, public static final void main(String[] args){}.

53) What is Runtime Polymorphism?

  • Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

  • In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

    Example:

54) Can you achieve Runtime Polymorphism by data members?

No.

举例说明多态

class Animal {
    public void eat() {}
}

class Dog extends Animal {
    public void eat() {}

    public void watchDoor() {

    }
}

class Cat extends Animal {
    public void eat() {}

    public void playGame() {}
}

class Demo {
    public static void main(String[] args) {
       Animal a = new Dog();
       a.eat();
       // 内存中是狗
       Dog d = (Dog) a;
       d.eat();
       d.watchDoor();

       // 内存中是猫
       a = new cat();
       Cat c = (Cat) a;
       c.eat();
       c.playGame();
    } 
}

55) What is the difference between static binding and dynamic binding?

In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.

56) What is abstraction?

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it.

57) What is the difference between abstraction and encapsulation?

Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.

58) What is abstract class?

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

59) Can there be any abstract method without abstract class?

No, if there is any abstract method in a class, that class must be abstract.

60) Can you use abstract and final both with a method?

No, because abstract method needs to be overridden whereas you can't override final method.

61) Is it possible to instantiate the abstract class?

No, abstract class can never be instantiated.

62) What is interface?

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.

63) Can you declare an interface method static?

No, because methods of an interface is abstract by default, and static and abstract keywords can't be used together.

64) Can an Interface be final?

No, because its implementation is provided by another class.

65) What is marker interface?

An interface that have no data member and method is known as a marker interface.For example Serializable, Cloneable etc.

66) What is difference between abstract class and interface?

67) Can we define private and protected modifiers for variables in interfaces?

No, they are implicitly public.

68) When can an object reference be cast to an interface reference?

An object reference can be cast to an interface reference when the object implements the referenced interface.

69) What is package?

A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.

70) Do I need to import java.lang package any time? Why ?

No. It is by default loaded internally by the JVM.

71) Can I import same package/class twice? Will the JVM load the package twice at runtime?

One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.

72) What is static import ?

By static import, we can access the static members of a class directly, there is no to qualify it with the class name.

PreviousSec 1: Basics of Java QuestionsNextSec 3: Exception Handling

Last updated 5 years ago

Was this helpful?

(2) Program of counter by static variable

Yes, you can have many main() methods in a class by overloading the main method.