Basic java programs

  1. Print prime numbers? 
  2. Interfaces allows constructors? 
  3. Can we create static constructor in java 
  4. Super keyword interview questions java 
  5. Java interview questions on final keyword
  6. Can we create private constructor in java
  7. Java Program Find Second highest number in an integer array 
  8. Java interview programming questions on interfaces 
  9. Top 15 abstract class interview questions  
  10. Java interview Questions on main() method
  11. Java Interview Program to find smallest and second smallest number in an array 
  12. Java Coding Interview programming Questions : Java Test on HashMap  
  13. Explain java data types with example programs 
  14. Constructor chaining in java with example programs 
  15. Swap two numbers without using third variable in java 
  16. Find sum of digits in java 
  17. How to create immutable class in java 
  18. AtomicInteger in java 
  19. Check Even or Odd without using modulus and division  
  20. String Reverse Without using String API 
  21. Find Biggest substring in between specified character
  22. Check string is palindrome or not?
  23. Reverse a number in java?


  24. Fibonacci series with Recursive?
  25. Fibonacci series without using Recursive?
  26. Sort the String using string API?
  27. Sort the String without using String API?
  28. what is the difference between method overloading and method overriding?
  29. How to find largest element in an array with index and value ?
  30. Sort integer array using bubble sort in java?
  31. Object Cloning in java example?
  32. Method Overriding in java?
  33. Program for create Singleton class?
  34. Print numbers in pyramid shape?
  35. Check armstrong number or not?
  36. Producer Consumer Problem?
  37. Remove duplicate elements from an array
  38. Convert Byte Array to String
  39. Print 1 to 10 without using loops
  40. Add 2 Matrices
  41. Multiply 2 Matrices
  42. How to Add elements to hash map and Display
  43. Sort ArrayList in descending order
  44. Sort Object Using Comparator
  45. Count Number of Occurrences of character in a String
  46. Can we Overload static methods in java
  47. Can we Override static methods in java 
  48. Can we call super class static methods from sub class 
  49. Explain return type in java 
  50. Can we call Sub class methods using super class object? 
  51. Can we Override private methods ? 
  52. Basic Programming Questions to Practice : Test your Skill

Top 10 Java interview questions on final keyword

1. What is the use of final keyword in java?   


  • By using final keyword we can make
  • Final class
  • Final method
  • Final variables
  • If we declare any class as final we can not extend that class
  • If we declare any method as final it can not be overridden in sub class
  • If we declare any variable as final its value unchangeable once assigned.

2. What is the main difference between abstract method and final method?

  • Abstract methods must be overridden in sub class where as final methods can not be overridden in sub class
3. What is the actual use of final class in java?

  • If a class needs some security and it should not participate in inheritance in this scenario we need to use final class.
  • We can not extend final class.
4. What will happen if we try to extend final class in java?
  • Compile time error will come.
  1. package com.finalkeywordintweviewprograms;
  2.  public final  Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.finalkeywordintweviewprograms;
  2.  public Class Sample  extends SuperDemo{  //The type Sample cannot subclass the final class
  3. SuperDemo

  4. }

5.Can we declare interface as final?

  • No We can not declare interface as final because interface should be implemented by some class so its not possible to declare interface as final.
final keyword interface in java




6. Is it possible to declare final variables without initialization?

  • No. Its not possible to declare a final variable without initial value assigned.
  • While declaring itself we need to initialize some value and that value can not be change at any time.

final variable in java
  1. package com.finalkeywordintweviewprograms;
  2. public final  Class Sample{ 
  3.  
  4. final int x=12,y=13;
  5.  
  6. public void Method() {

  7. x=25;// compile time error:The final field Super.x cannot be assigned
  8. y=33;// compile time error: The final field Super.y cannot be assigned
  9.  
  10. }
  11.  
  12. }

7. Can we declare constructor as final?
  • No . Constructors can not be final.

8.What will happen if we try to override final methods in sub classes?

  • Compile time error will come :Cannot override the final method from Super class
9.Can we create object for final class?
  • Yes we can create object for final class. 
10.What is the most common predefined final class object you used in your code? 
  • String (for example)

Super keyword interview questions java

Super Keyword:


  • The functionality of super keyword is only to point the immediate super class object of the current object.
  • super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
  • super keyword used to access the members of the super class object.
  • super.member;
  • It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.
  • We can call super class constructor in sub class using super() call.
  • We can access super class methods and variables in sub class using super.variable_name, super.method();

Uses of super : 

 
1. By using super keyword we can access super class variables in sub class.

  • Using super keyword we can access super class variables from sub class.
  • super.variable_name.
  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. int a,b;
  5.  
  6. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7. super.a=10;
  8. super.b=20;
  9.  
  10. System.out.println(super.a);
  11. System.out.println(super.b);
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  Subdemo obj= new Subdemo();
  17.  
  18. obj.a=1;
  19. obj.b=2;
  20.  
  21. obj.disply();
  22.  

  23.  
  24. }
  25. }

Output:

  1. 1
  2. 10
  3. 20

2. By using super keyword we can access super class methods in sub class.
  • Using super keyword we can call super class methods from sub class.
  • super.method();.

  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }

  1. package com.instanceofjavaforus;
  2. public Class Subdemo extends SuperDemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7.  
  8. super.a=10;
  9. super.b=20;
  10.  
  11.  super.show();
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  
  17.  Subdemo obj= new Subdemo();
  18.  
  19. obj.a=1;
  20. obj.b=2;
  21.  
  22. obj.disply();

  23.  
  24. }
  25. }

Output:

  1. 1
  2. 10
  3. 20
 
3. We can call super class constructor from class constructor:
  •  By using super keyword we can able to call super class constructor from sub class constructor.
  • Using super(); 
  • For  the super(); call must be first statement in sub class constructor.  

  1. package com.superinterviewprograms;
  2. public Class SuperDemo{ 
  3.  
  4. int a,b;
  5.  
  6. SuperDemo(int x, int y){
  7.  a=x;
  8. b=y
  9. System.out.println("Super class constructor called ");
  10.  
  11.  
  12. }



  1. package com.superinterviewprograms;
  2.  
  3. public Class Subdemo extends SuperDemo{ 
  4.  
  5. int a,b;
  6.  
  7. SubDemo(int x, int y){
  8. super(10,20);
  9.  a=x;
  10. b=y
  11. System.out.println("Sub class constructor called ");
  12. }
  13.  
  14. public static void main (String args[]) {
  15.  Subdemo obj= new Subdemo(1,2);

  16.  
  17. }
  18. }

Output:
  1. Super class constructor called
  2. Sub class constructor called 

key points:

  • Super(); call must be first statement inside constructor.
  • By default  in every class constructor JVM adds super(); call in side the constructor as first statement which calls default constructor of super class, if we are not not calling it.
  • If we want to call explicitly we can call at that time default call wont be there.

4.What will happen if  we are calling super() in constructor but our class does not extending any class?

  •  if our class not extending any class Yes still we can use super(); call in our class 
  • Because in java every class will extend Object class by default this will be added by JVM.
  • But make sure we are using only super(); default call we can not place parameterized super call because Object class does not have any parameterized constructor.

  1. package com.superinterviewprograms;
  2.  
  3. public Class Sample{ 
  4.  
  5. Sample(){
  6. super();
  7. System.out.println("Sample class constructor called "); 
  8.  
  9. }
  10.  
  11. public static void main (String args[]) {
  12.  
  13.  Sample obj= new Sample();
  14.  
  15. }
  16. }

Output:

  1. Sample class constructor called


5.What if there is a chain of extended classes and 'super' keyword is used


  1. package com.superinterviewprograms;
  2. public Class A{ 
  3.  
  4. A(){
  5.  System.out.println("A class constructor called ");
  6.  
  7.  
  8. }

  1. package com.superinterviewprograms;
  2. public Class B extends A{ 
  3.  
  4. B(){
  5.  
  6.  System.out.println("B class constructor called ");
  7.  
  8.  
  9.  
  10. }

  1. package com.superinterviewprograms;
  2. public Class C extends B{ 
  3.  
  4. C(){
  5.  System.out.println("C class constructor called ");
  6.  
  7.  
  8. public static void main (String args[]) {
  9.  C obj= new C();

  10.  
  11. }
  12. }

Output:

  1. A class constructor called
  2. B class constructor called
  3. C class constructor called

6. Can we call super class methods from static methods of sub class?

  • No we can not use super in static methods of sub class Because super belongs to object level so we can not use super in static methods. 
  • If we try to use in sub class static methods compile time error will come.

super keyword in java interview example program frehser

Data types in java with example programs

  • The data type is something which gives information about
  • Size of the memory location and range of the data that can be accommodated inside that location.
  • Possible legal operations those can be performed on that location.
  • What type of result come out from an expression when we use these types inside that expression.
  • Whichever the keyword gives these semantics is treated as "data type".
  • The java language provides different categories of data types. 

1.Primitives Data types:

  • The primitive data types are the predefined data types given by the programming language and they are meant for storing a single value.
  • Based on the type and range of data, primitive types are divided into 8 types.

1.Integer category: 

  • This category can be used for storing numbers which can be either positive value  or negative value without decimal points.
  • In this category we have 4 primitive data types whose memory sizes are different. 
  • All the 4 types under this category are used for storing same data. But there ranges are different. The java language is providing 4 types under Integer category. So that the memory is utilized efficiently. 
  1. byte  
  2. short
  3. int
  4. long 
data types in java


byte data type in java:

  • Size 1 byte. i.e 8 bits
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (2^7 -1)
  • Default value is 0
Java example program on byte data type:

  1. package com.instanceofjava;
  2. Class ByteDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  byte x=10;
  7.  byte y=20;
  8.  
  9.   byte z= (byte) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30 

 short data type in java:

  • Size 2 bytes.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (2^15-1)
  • Default value is 0

Java example program on short data type:

  1. package com.instanceofjava;
  2. Class ShortDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  short x=10;
  7.  short y=20;
  8.  
  9.   short z= (short) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30

int data type in java:

  • Size 4 bytes.
  • Minimum value is - 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647 (2^31 -1)
  • Default value is 0

Java example program on int data type:

  1. package com.instanceofjava;
  2. Class IntDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9.   int z=x+y;
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30

long data type in java:
  • Size 8 bytes.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (2^63 -1)
  • Default value is 0

Java example program on long data type:

  1. package com.instanceofjava;
  2. Class LongDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  long x=109876677777l;
  7.  long y=20666677766l;
  8.  
  9.  long z=x+y;
  10.  
  11.  System.out.println(z);
  12.  
  13. }

  14. }



Output:

  1. 130543355543

2.Floating point category: 

  • This category can be used for storing numbers which can be either +VE or –VE with decimal point. In the floating point category we have two types whose size is different. The two data types are float and double.
  • Both the data types under the floating point category are used for storing same data but there range is different. The java language provide as two data types under floating point category so that memory is utilized efficiently. 
  • Default value of float is 0
  • Default value of double is 0.0 
  1. float
  2. double


float data type in java

float data type in java:

  • Size 4 bytes.
  • Minimum value is 1.4e-45
  • Maximum value is 3.4e38
  • Default value is 0.0f

Java example program on float data type:

  1. package com.instanceofjava;
  2. Class FloatDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  float x=231231.0031f;
  7.  float y= 53423423434.43231f;

  8.  float z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 5.3423653E10

double data type in java:
  • Size 8 bytes.
  • Minimum value is 4.9e-324
  • Maximum value is 1.8e308
  • Default value is 0.0d

Java example program on double data type:

  1. package com.instanceofjava;
  2. Class DoubleDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  double x=231231.0031f;
  7.  double y= 53423423434.43231f;

  8.  double z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 5.342365466543541E10

3.Character category: 

  • This category can be used for storing a single character. A character can be represented by alphabets, a digit and special symbols. 
  • This category contains only one data type an it is char.
  • Default value of char is "one space"
  1. char

char data type in java:
  • Size 2  bytes.
  • Minimum value is 0
  • Maximum value is 65,535
  • Default value is ' '

Java example program on char data type:

  1. package com.instanceofjava;
  2. Class CharDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  char a='a';
  7.  System.out.println(a);
  8.  
  9. }

  10. }


Output:

  1. a

4.Boolean category: 

  • This category can be used for storing either true or false. Under the Boolean category only one data type an it is Boolean. The size is dependent on JVM to JVM.

boolean data type in java:

  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false. '

Java example program on boolean data type:

  1. package com.instanceofjava;
  2. Class BooleanDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. boolean x = false;
  7. System.out.println(x);
  8.  
  9. }

  10. }

Output:

  1. false

Top 10 interview questions on java interfaces

1.What is an interface in Java.

  • Before Java 8 interfaces are pure abstract classes which allow us to define public static final variables and public abstract methods(by default).
  • In java 8 introduced static methods and default methods in interfaces. 
  • We can develop interfaces by using "interface" keyword.  
  • A class will implements all the methods in an interface.



  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x=12;
  5. void show(); 

  6. }

  1. package com.instanceofjava;
  2. Class A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }

  7. }



  1. package com.instanceofjava;
  2. interface Java8Interface{
  3.  
  4. int x=12;
  5. void show(); 

  6.   
  7. default void display(){
  8.  
  9. System.out.println("default method of interface");
  10.  
  11. }
  12.  
  13. Static void print(String str){
  14.  
  15. System.out.println("Static method of interface:"+str);
  16.  
  17. }
  18.  
  19.  
  20. }




2.What will happen if we define a concrete method in an interface?
  • By default interface methods are abstract.
  • if we declare any concrete method in an interface compile time error will come.
  • Error:Abstract methods do not specify a body

Interface concrete method in java


3.Can we create non static variables in an interface?

  • No.We can not create non static variables in an interface.
  • If we try to create non static variables compile time error comes.
  • By default members will be treated as public static final variables so it expects some value to be initialized.

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error


  5. }


4.What will happen if we not initialize variables in an interface.
  • Compile time error will come because by default members will be treated as public static final variables so it expects some value to be initialized.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error: The blank final field y may not have been initialized


  5. }
5.Can we declare interface members as private or protected?
  • No.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. private int x; // compile time error: Illegal modifier for the interface field Sample.x; only
  5. public, static & final are permitted
  6. protected int a; // compile time error: Illegal modifier for the interface field Sample.a; only
  7. public, static & final are permitted

  8. }


7.When we need to use extends and implements?
  • A class will implements an interface.
  • A class will extends another class.
  • An interface extends another interface.

6.Can we create object for an interface?

  • NO. We can not create object for interface.
  • We can create a variable fro an interface

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  5. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }
  7. public static void main(String args[]){
  8.  
  9.  JavaInterface obj= new JavaInterface(); // Error: Cannot instantiate the type JavaInterface
  10.  
  11. }
  12. }

7.Can we declare interface as final?

  • No. Compile time error will come.
  • Error: Illegal modifier for the interface Sample; only public & abstract are permitted
declare interface final in java



8.Can we declare constructor  inside an interface?

  • No. Interfaces does not allow constructors.
  • The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.

final interface interview freshers


9.What will happen if we are not implementing all the methods of an interface in class which implements an interface?

  • A class which implements an interface should implement all the methods (abstract) otherwise compiler will throw an error.
  • The type Example must implement the inherited abstract method JavaInterface.show() 
  • If we declare class as abstract no need to implement methods. 
  • No need of overriding default and static methods.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  • }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface { // The type Example must implement the inherited
  3. abstract method JavaInterface.show()
  4.  
  5. public static void main(String args[]){
  6.  

  7. }
  8. }

10.How can we access same variables defined in two interfaces implemented by a class?

  • By Using corresponding interface.variable_name we can access variables of corresponding interfaces.
11.If  Same method is defined in two interfaces can we override this method in class implementing these interfaces.
  • Yes implementing the method once is enough in class.
  • A class cannot implement two interfaces that have methods with same name but different return type.

Hibernate configuration file

1.hibernate.dialect:
  • This property hibernate.dialect makes Hibernate generate the appropriate SQL for the given database.
2.hibernate.connection.driver_class:
  • This property hibernate.connection.driver_class specifies jdbc driver class name 
3.hibernate.connection.username:
  • This property hibernate.connection.username specifies database user name.
4.hibernate.connection.password:
  • This property hibernate.connection.password specifies password.



hibernate.cfg.xml file structure


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6.  
  7. <session-factory>
  8.  
  9. <!-- Related to the connection START -->
  10. <property name="connection.driver_class">Driver Class Name </property>
  11. <property name="connection.url">URL </property>
  12. <property name="connection.user">user name</property>
  13. <property name="connection.password">password</property>
  14. <!-- Related to the connection END -->

  15. <!-- Related to hibernate properties START -->
  16. <property name="show_sql">true/false</property>
  17. <property name="dialet">Database dialet class</property>
  18. <property name="hbm2ddl.auto">create/update or what ever</property>
  19. <property name="hibernate.jdbc.batch_size">hibernate container that every N rows to be
  20. inserted as batch.</property> 
  21. <!-- Related to hibernate properties END-->
  22.  
  23. <!-- Related to mapping START-->
  24. <mapping resource="hbm file 1 name .xml" />
  25. <mapping resource="hbm file 2 name .xml" />
  26. <!-- Related to the mapping END -->
  27.  
  28. </session-factory>
  29.  
  30. </hibernate-configuration>




Example hibernate.cfg.xml. file  for mysql


hibernate configuration file java

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="connection.url"> jdbc:mysql://localhost/test</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.MySQLDialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>


Example hibernate.cfg.xml. file for oracle Db

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  9. <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.Oracle9Dialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>



Select Menu