Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 27 September 2017

How to Convert Decimal to Hexadecimal in Java


In the last post we saw how to convert decimal number into binary number in java but here we will learn how to convert decimal to hexadecimal in java.
How to convert decimal to hexadecimal in java


Let's start java program to convert decimal to hexadecimal.

Convert Decimal to hexadecimal in java

We can convert decimal number to hexadecimal number in java by two ways and these are given below.
  • By using toHexString() method of Integer class which is wrapper class.
  • By writing your own logic, without using any predefined method.

(1) Let's write a java program to convert a decimal number to hexadecimal number using "toHexString()" method of Integer class.

class DecimalToHexadecimal
{
public static void main(String args[])
{
int i = 32;

String str = Integer.toHexString(i);
System.out.println("After conversion hexadecimal number is: "+str);
}
}

Output: After conversion hexadecimal number is 20

                                                       Or

This is the same example as above but here we will use Scanner class so that we can take input from the users.

import java.util.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int number = input.nextInt();
//using toHexString() method of Integer class
String str = Integer.toHexString(number);
System.out.println("Hexadecimal number is: "+str);
}
}

Output: Enter a decimal number:
             45
             Hexadecimal number is: 2d

(2) Program to convert decimal to hexadecimal in java without using any ready made method. Here we will put own logic for conversion.

import java.util.Scanner.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
//taking input from the user
System.out.println("Enter decimal number: ");
int number = input.nextInt();
int rem;//for storing remainder
String str = "";//for storing result
char hexa[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(number>0)
{
rem = number%16;

str = hexa[rem]+str;
number = number/16;
}
System.out.println("Hexadecimal Number is: "+str);
}
}

Output: Enter decimal number:
             45
             Hexadecimal Number is: 2D


How to convert Hexadecimal to decimal number in java?

This is the simple example where we are going to convert hexadecimal number to Decimal number in java. In this example we will use parseInt() method of Integer wrapper class.

class HexaToDecimal
{
public static void main(String args[])
{
//String containing hexadecimal number
String hexanumber = "20";
//we will use parsInt() method of Integer wrapper class
int decimalnumber = Integer.parseInt(hexanumber, 16);//pass 16
System.out.println("hexadecimal number will be convert into decimal");
System.out.println("Decimal number is: "+decimalnumber);
}
}

Output: hexadecimal number will be convert into decimal
             Decimal number is: 32

Above all the examples are quite simple and easy and it is very useful examples. There are many java number conversion interview questions are asked in java or core java interviews e.g.... 

how to convert decimal to binary number with example
how to convert decimal to hexadecimal number with example.
how to convert decimal to octal number in java with example.
how to convert binary to decimal with example.
how to convert binary to hexadecimal. 
how to convert binary to octal with example.
how to convert hexadecimal to binary, decimal, octal.

and many more...but above we saw simple examples of how to convert decimal to hexadecimal and conversion of hexadecimal to decimal number in java.
Share:

Tuesday 26 September 2017

Interview Questions on Abstraction in Java

Java Abstraction Interview Questions

Abstraction Interview Questions in java

Now, Here we are going to discuss the most important core java interview questions and answers which is "interview questions on abstraction in java".

So let's start with abstraction interview questions.


(1) What is abstraction in java?

In java, Abstraction means show functionality and hide complexity or internal details or hide implementation details to the user is know as abstraction in java.

For example:

The best example in the world of abstraction is ATM machine where we can withdraw or transfer money easily but how it happens? we don't know. We don't know internal details or implementation details.


(2) How to achieve abstraction in java?

There are two ways in java we can achieve abstraction.
  • By using abstract class(0 to 100%).
  • By using interface(100%).


(3) What is abstract class in java?

When we declared any class with "abstract" keyword is known as abstract class. In abstract class we can keep abstract method(without body) and non-abstract method(with body).

For example:

abstract class Demo
{
abstract void show();//abstract method
void show(){}//non-abstract method
}

(4) Can we create instance of abstract class?

No, We cannot create an instance of an abstract class.

(5) Can we define abstract class without abstract method?

Yes, We can define abstract class without abstract method.

(6) Can we declare abstract method in non-abstract class?

No, We can't declare abstract method in non-abstract class.

For example:

class Demo
{
abstract void show();
}

Above example will throw compile time error.


(7) What is interface in  java? 

An interface in java is a blueprint of a class that have static constants and abstract method by default.

(8) Why we use interface in java?

In java we use interface so that we can achieve fully abstraction because through abstract class we can't achieve full abstraction.


(9) Can we create instance of interface?

No, We cannot create object of both an interface and abstract class.

(10) Can we declare abstract method as static?

No, We can't use static keyword with abstract method.

(11) Can we declare abstract method as final?

No, We cannot use final keyword with abstract class.


(12) Can we declare abstract method as private?

No, We cannot declare abstract method as private.

(13) Can we use public, protected and default modifiers with abstract method?

Yes, We can use public, protected and default modifiers with abstract method.

(14) Can we declare local inner class as abstract?

Yes, We can declare local inner class as abstract.

(15) Method in interface are by default public and abstract. true or false?

Yes, It is true.

(16) Data member in interface are by default public, static, and final. true of false?

Yes, It is true.

(17) Can abstract class implements interface in java?

Yes, Abstract class can implement interface.

(18) Can we use abstract keyword with constructor?

No, We can't use abstract keyword with constructor.

(19) Abstract classes can be nested. true or false?

Yes, Abstract classes can be nested.


(20) Can abstract class have constructor in java?

Yes, We can declare constructor in abstract class.

(21) Difference between abstraction and encapsulation in java?


(22) What is the difference between abstract class and interface?

You can click on this link to see what is the difference between abstract class and interface in java with example 


(23) Can abstract method declaration include throws clause?

Yes.


(24) What will happen if we do not override all the abstract methods in sub-class?

It will throw compile-time error. We have to override all the abstract method in sub-class. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.

Read More:

Java Interview Questions on Interface.
Java OOPS Interview Questions.
Java Inheritance Interview Questions.
Java Super Keyword Interview Questions.
Java Abstract Class with Example.
Java Interface with Example.

This java abstraction interview questions will help you in java interviews. 

Share:

Sunday 24 September 2017

Java Programs on Increment and Decrement Operators

Increment and Decrement Operators in Java

Increment and Decrement Operators in java with examples

In this post, We will see some basic java programs on increment and decrement operators.

Let's start, Increment and decrement operators in java with examples.


Program 1

class Test1
{
public static void main(String args[])
{
int a, b;
a = 30;
b = ++a;
System.out.println(b);
}
}

Output: 31


Program 2

class Test2
{
public static void main(String args[])
{
int a,b;
a = 30;
b = a++;
System.out.println(b);
System.out.println(a);
}
}

Output: 30
              31


Program 3

class Test3
{
public static void main(String args[])
{
int a = 40;
a = ++a + 2;
System.out.println(a);
}
}

Output: 43


Program 4


class Test4
{
public static void main(String args[])
{
int a = 50;

a = a++ + 1;
System.out.println(a);
}
}

Output: 51


Program 5

class Test5
{
public static void main(String args[])
{
int x = 10;
x = ++x + ++x;
System.out.println(x);
}
}

Output: 23


Program 6

class Test6
{
public static void main(String args[])
{
int a = 30;
a = a++ + a++;
System.out.println(a);
}
}

Output: 61


Program 7

class Test7
{
public static void main(String args[])
{
int x = 5;
int y = 21;
int z = ++x *7 + 2 - y--;
System.out.println("z = "+z);
}
}

Output: 23


Program 8

class Test8
{
public static void main(String args[])
{
int a = 30;
a = a++ + ++a;
System.out.println(a);
}
}

Output: 62


Program 9

class Test9
{
public static void main(String args[])
{
int a,b;
a = 50;
b = --a;
System.out.println(b);
}
}

Output: 49


Program 10

class Test10
{
public static void main(String args[])
{
int x,y;
x = 40;
y = x--;
System.out.println(x);
}
}

Output: 39


Program 11

class Test11
{
public static void main(String args[])
{
int z = 40;
z = z-- + ++z;
System.out.println(z);
}
}

Output: 80


Program 12

class Test
{
public static void main(String args[])
{
int x = 1;
System.out.println(a++);
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
}
}

Output: 1
              2
              4
              4
              2

Visit below links for more practice 

Java Array Programming Interview Questions.
Java String Programming Interview Questions.


Above examples are basic increment and decrement operator examples in java.
Share:

Thursday 21 September 2017

How to Convert Decimal to Binary in Java

Decimal to Binary Conversion in Java

Java Decimal to Binary Conversion

Here we will learn, How to convert decimal to binary in java with easy examples. Java decimal to binary conversion is the most important core java interview question.

There are many number conversion interview questions which is asked in java interviews like java program to convert decimal to hexadecimal, java program to convert decimal to octal, java program to convert binary to decimal, etc. But here we learn only decimal to binary conversion program in java. Let's start...

First, Let us understand what is decimal and binary number.

Decimal Numbers - Decimal number is the real number using the base 10. There are only 10 digits which represents numbers and starting from 0 to 9.

Binary Numbers - Binary number is number which contains only two digits 0 and 1 which know as bits.

Binary numbers in computer language i. e 0 and 1 represents true/false or on/off.

There are 3 ways, we can convert decimal to binary in java programs.

  • First is, Using toBinaryString() method of Integer class.
  • Second is, Using you own logic without any predefined method.
  • Third is, Using Stack. 


1) Decimal to Binary Example Using toBinaryString() method

class DecimalBinary
{
public static void main(String args[])
{
System.out.println("Binary representation of 1: ");
System.out.println(Integer.toBinaryString(1));
System.out.println("Binary representation of 6: ");
System.out.println(Integer.toBinaryString(6));
System.out.println("Binary representation of 12: ");
System.out.println(Integer.toBinaryString(12));
System.out.println("Binary representation of 45: ");
System.out.println(Integer.toBinaryString(45));
}
}

Output: Binary representation of 1:
              1
             Binary representation of 6:
             110
             Binary representation of 12:
             1100
             Binary representation of 45:
             101101


2) Decimal to Binary Example and count the number of 1s in binary numbers

In this program, we will take the input from user and convert it decimal to binary number and count 1s.

import java.util.Scanner;
class Counts
{
public static void main(String args[])
{
int n, a, count = 0;
String s = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number ");
n = sc.nextInt();
while(n>0)
{
a = n%2;
if(a == 1)
{
count++;
}
s = s+" "+a;
n = n/2;
}
System.out.println("Binary number: "+s);
System.out.println("No. of 1s: "+count);
}
}

Output: Enter decimal number
             45
             Binary number: 101101
             No of 1s: 4


3) Write a java program to convert decimal to binary without using predefined method

class Test
{
public void binaryConvert(int num)
{
int bin[] = new int[45];
int index = 0;
while(num > 0)
{
bin[index++] = num%2;
num = num/2;
}
for(int i = index-1; i >= 0; i--)
{
System.out.print(bin[i]);
}
}
public static void main(String args[])
{
Test t = new Test();
System.out.println("Binary representation of 123: ");
t.binaryConvert(123);
}
}

Output: Binary representation of 123:
             1111011


4) Another Example of java decimal to binary conversion

In this program, we create stack object and take input from the user for converting decimal to binary.

import java.util.*;
class Demo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//creating Stack object
Stack<Integer> stack = new Stack<Integer>();
System.out.println("Enter the decimal number: ");
int num = sc.nextInt();
while(num != 0)
{
int b = num % 2;
stack.push(b);
num /= 2;
}
System.out.print("\n Binary is: ");
while(!(stack.isEmpty()))
{
System.out.print(stack.pop());
}
System.out.println();
}
}

Output: Enter the decimal number: 
             65
             Binary is: 1000001

Read More:

How to Convert Binary to Decimal in Java.
How to convert Decimal to Hexadecimal in Java.
How to Convert Binary to Hexadecimal in Java.


Above all the examples of decimal to binary conversion programs in java are mostly asked in java interviews.

Share:

Wednesday 20 September 2017

Interview Questions and Answers on Inheritance in Java

Java Inheritance Interview Questions

Java Inheritance Interview Questions and Answers

Now, Here we are going see some most important interview questions and answers on inheritance in java. This is the most important topic from the core java interview point of view.

There are many oops concepts interview questions are asked in core java interviews and Java inheritance concept is one of them.


(1) What is inheritance in java?

Inheritance is the oops concept and in java inheritance means re-usability. By using this inheritance concept a child class can inherit its parent class so that the child class can reuse all the field and methods of its parent class.


(2) What are the types of inheritance?

There are 5 types of inheritance. Suppose there is a class A, B, C, and D.

1) Single level inheritance.

B extends A

2) Multilevel inheritance.

C extends B and B extends A

3) Multiple inheritance.

C extends B and A

4) Hierarchical inheritance.

C  and B extends A  

5) Hybrid inheritance.

D extends C and B and C and B extends A


(3) Use of inheritance in java?

There are many reasons for using inheritance in java and these are given below.
  1. To achieve dynamic binding or method overriding.
  2. For code re-usability.
  3. To save time.

(4) Does java support multiple inheritance?

No, Java doesn't support multiple inheritance because of complexity and ambiguity in a program. Java does not support multiple inheritance through the class but it is possible through the interface.

(5) What is the syntax of inheritance?

There is the syntax of inheritance.

//Single levele inheritance
class A
{
//data member;
//member function
}
class B extends A
{
//data member;
//member function
}
}


(6) What is IS-A relationship in inheritance?

Java inheritance represents is-a relationship and parent-child relationship is also know as is-a relationship.


(7) What is aggregation in java?

If a class have an entity reference, it is known as aggregation and aggregation represents Has-a relationship.

For example 

class Student
{
String name;
Address  address;//Adderess is a class
}

Here Address is a class which may contain other information like city, country, etc.


(8) By default, all the classes extend which class in java?

In java, All the classes extend Object class by default because Object class is the super class in java.


(9) How inheritance can be implemented in java?

By two ways we can implement inheritance.

  • Using extends keyword.
  • Using implements keyword.

(10) How do you implement multiple inheritance in java?

We can implement multiple inheritance in java by using interface concept. A class can implement multiple interface at the same time but a class can't extend more than one class at a time.

For example:

interface My
{
//method
}
interface My1
{
//method
}
class Test implements My,My1
{}


(11) Can a class extend itself?

No, A class can't extend itself.


(12) Are interfaces extend Object class by default?

As we know that all the classes extend Object class by default but the interface does not extend Object class by default.


(13) Can you inherit a final class in java?

No, We can't extend the final class.


(14) Can you inherit final method of super class into a sub-class?

Yes, we can inherit final method of a super class into a sub-class.


(13) Can you inherit private members of super class into a sub-class?

No, we cannot inherit private member of a super class into a sub-class.


(14) Can we inherit constructor in sub-class?

No, we cannot inherit constructor in sub-class.


(15) What happens if super class and sub class having same field name?

Super class field will be hidden in the sub class but you can access hidden field of a super class by using 'super' keyword in the sub-class.


(16) Can you inherit static member into a sub-class.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate