Data Types and Variables in Java Important Question Answer

Java Important Question Answer

Data Types and Variables

Que 1. What are Variables?

Answer: A variable is used to store the data. A Variable is a placeholder for data that can change its value during program execution.
Technically, a variable is a name for a storage location in the computer’s internal memory.

Que 2. What do you mean by the value of the variable?

Answer: The value of the variable is the contents at that location.

Que 3. What are the characteristics of variable in Java?

Answer: Characteristics of Variable in Java

  • All data variables in Java have to be declared and initialized before they are used.
  • Variable names have to be a single word. Use underscore _ to represent space in a variable name. Underscore is not a space.
  • When declaring variables, we have to specify the data type of variable, which tells the type of information/value a variable can store.

Que 4. What do you mean by variable declaration?

Answer: Variable declaration means declaring the data types and names of variables before using them.

Syntax:

data_type variable_name;

datatype variablename;

int rate_of_interest;

float rate;

float rateofinterest;

Que 5. What is the importance of data type at the time of variable declaration?

Answer: The data types give the following information to the compiler:

(a) The types of values can be stored by the variable like integer, float, character, boolean, etc.

(b) The type of a variable tells the compiler, how much memory to reserve when storing a variable of that type.

(c) About what types of operations are possible on the variable.

Que 6. All data variables in Java have to be ______ and ________ before they are used.

Answer: declared, initialized

Que 7. What do you mean by variable initialization?

Answer: Variable initialization means assigning a value to the variable.

Variable_name = value ;

rate_of_interest = 20 ;

rate = 10.25;

Que 8. Help Mr. Amrit to do the following:

(a) Declare a variable to store the value of his salary.

Answer:        double salary = 458000.00; // declaration & initialization

or

double salary; // declaration

salary = 458000; // initialization

(b) Declare a variable to store the gender of Sunita.

Answer: char gender = ‘F’;

(c) Declare a variable to store the true value of his marital status.

Answer: boolean marital_status = true;

(d) Declare a variable to store the age of Sunita.

Answer: int age = 25;

(e) Declare a variable to store the percentage marks of Sunita.

Answer: double percentage = 95.0 ;

Que 9. What are local variables?

Answer: Variables that are declared inside the method, are called local variables. These variables are available only inside the methods.

Que 10. What is data type?

Answer: Data type specify the type of the data and associated operation.

The data type is used in the

  • declaration of a variable,
  • specifying the return_type of methods,
  • specifying the type of arguments/parameters to the function, etc.

Que 11. What are primitive data types? How many types of primitive data types in Java?

Answer: The data types given by the programming language, i.e. pre-defined / built-in data types, are known as primitive data types.

In Java, there are 8 types of primitive data types. These are –

(i) Numeric Integer Type

(a) byte,

(b) short,

(c) int,

(d) long,

(ii) Numeric Float type

(e) float,

(f) double,

(iii) Character Type

(g) char,

(iv) Boolean Type

(h) boolean

Que 12.  Specify the size of all primitive data types.

Answer: Memory Size of Primitive Data Types

byte                –          8 bits (1 byte),

short              –          16 bits (2 bytes),

int                   –          32 bits (4 bytes),

long                –          64 bits (8 bytes),

float               –          32 bits (4 bytes),

double           –          64 bits (8 bytes),

char                –          16 bits (2 bytes) and

boolean         –          1 bit (1 byte of memory i.e. 8 bits)

Que 13. Write the rules for naming variables. [CBSE]

Answer: Rules for writing variable names/function names are –

            (a) Variable names can consist of alphabets, digits, _ (underscore), and $ (dollar)

(b) Variable names can begin with either an alphabetic character, an underscore (_), or a dollar sign ($).

(c) Space is not allowed in the variable names.

(d) Keywords cannot be used as a variable name.

(e) Java is a case-sensitive language, Variable names written in capital letters differ from variable names with the same spelling but written in small letters.

Que 14.  Give the example of three valid variable names?

Answer: total_marks, marks4, marks$, _marks, marks, Int

Que 15.  Give the example of three invalid variable names? Give reason.

Answer:  Invalid variables with reason are

  • total marks   –          using of space in the name, which is not allowed.
  • 4marks          –           names only begin can begin with either an alphabetic character, an underscore (_), or a dollar sign ($).
  • marks#          –           special symbol except _ and $, not allowed
  • int                   –           keywords cannot be used as a variable name.

Que 16.  What are Character variables?

Answer: A variable that is declared as a char data type and stores only one character, is called a character variable.

To assign a value to a char variable we enclose the character between single quotes.

char first_name = ‘A’;

char last_name = ‘S’;

Que 17.  Write a java statement do declare a gender variable which store either ‘M’ or ‘F’.

Answer:        char gender = ‘M’;

Que 18.  What is a String?

Answer: A set of characters written inside the double quotes, called string in java. For example- “anjeev singh”, “mycstutorial.in”, “Your are 10 Years Old”, “25 jki @3 &^”, etc.

Que 19.  How can you create a String variable?

Answer: In Java String class is used to create a string variable i.e. string object.

String firstName = “my cs tutorial”;

String url = “www.anjeevsinghacademy.com”

Que 20.  What is the use of the + operator?

Answer: In Java, the + operator behaves in two ways –

(a) Addition: When used with numbers as operands, the + operator adds the numbers.

For example:

5 + 3 => 8

(b) Concatenation: When used with Strings as operands, the + operator concatenates the strings together.

For example:

“www.” + “mycstutorial.in” => “www.mycstutorial.in”

Que 21.  Write a java statement to print the value of age with appropriate message.

Answer:        int age = 20;

                        System.out.println(“Your age is”, +age);

Output is:

            Your age is 20

Que 22.  Write a program in java to calculate the percentage of marks and display the percentage with appropriate message.

Answer:       

public class PercentageCalculator {

            public static void main(String args[]) {

int maximum_marks = 400;

                        double marks_obtained = 368;

                        double percentage = 0.0;

                        percentage = ( marks_obtained / maximum_marks) * 100;

                        System.out.println(“Your percentage marks is”, +percentage);

            }

}

Output is:

Your percentage is 92

Que 23. Which of the following is an invalid variable declaration?  [SQP]

a) my_string_1                    b) 1st_string            c) mystring1             d) _mystring1

Answer:  b) 1st_string

Que 24. Write the name of data type which allows to store integer type value. Write their size also?

Answer: byte – 8 bits (1 Byte),  short – 16 bits (2 Bytes), int – 32 bits (4 Bytes) and long – 64 bits (8 Bytes)

Que 25.  Write a program in java to print the full name by using first_name, middle_name and last_name.

Answer:

public class FullNamePrinting {

            public static void main( String args[ ] ) {

                        String first_name = “Ravi” ;

                        String middle_name = “Kr”;

                        String last_name = “Singh”;

                        String full_name = first_name  + “ ” + middle_name + “ ” + last_name;

                        System.out.println(“Full Name is “, +full_name) ;

            }

}

Output is:

            Full Name is Ravi Kr Singh

Que 26.  Write one difference and one similarity between int and float data type?

Answer:

Difference: 

int data type allows to store the integer numeric value i.e. number without decimal point.

float data allows to store the real numeric value i.e. number with decimal point (fractional value)

Similarity:

Both consume 4 bytes / 32 bits of memory and both comes under the category of numeric data type.

Que 27.  Differentiate between float and double data type?

Answer: float is a single precision floating data type, while double is a double precision floating data type.

Que 28.  Differentiate between int and boolean data type?

Answer: Difference between int and boolean.

  • The int data type is used to store integer value, while the boolean data type is used to store boolean value i.e. true and false.
  • Int consumes 32 bits of memory while boolean consumes 1 bit of memory only.

 

Que 29. Write a java statement to create a byte variable and store value 20 to it.

Answer: byte number = byte(20);

 Que 30. What is type casting?

Answer: Type Casting is a process to convert the data type of variable or expression or value to another type explicitly.

For example:

byte number = byte(20);

// 20 is an integer value, In java every non-fractional number is by default integer.

//It need to convert to byte or short type if you want to convert it in respective type.

            int num = int(25.69);

Que 31. In Java byte, short, int and long, all of these are [CBSE 2017 COPTT]

(a) signed

(b) unsigned 

(c) both of the above

(d) none of these

Answer:  (c) both of the above

Que 32. What would be the output of the following fraction of code? [CBSE 2017 COMPT]

int Integer = 34 ;

char string = ‘S’ ;

System.out.print( Integer ) ;       

System.out.print( String ) ;

(a) Throws exception

(b) 34

(c) S

(d) 34S

Answer: (d) 34S

Que 33. Define Variable. Write the code to declare a variable to store your age. [CBSE 2018]

Answer: A variable is used to store the data. A Variable is a placeholder for data that can change its value during program execution. A variable is a name for a storage location in the computer’s internal memory.

int age = 25;

Que 34. An expression involving byte, int and literal numbers is promoted to which of these? [CBSE 2019]

(a) int

(b) long

(c) byte

(d) bloat

Answer: (a)  int

Que 35. What is data type?  Name the datatype used for storing fractional value in

any variable in JAVA? [CBSE 209]

Answer: Data type specify the type of the data and associated operation. The data type is used in the declaration of a variable, specifying the return_type of methods, specifying the type of arguments/parameters to the function, etc.

Datatype used for storing fractional value are: float and double

Que 36. Which data type value is returned by all transcendental math functions?  [CBSE 2019 COMPT]

(i) int

(ii) float

(iii) double

(iv) long

Answer: (iii) double

Que 37. Which of these statements is correct?  [CBSE 2019 COMPT]

(i) True and false are numeric values 1 and 0.

(ii) True and false are numeric values 0 and 1.

(iii) True is any non-zero value and false is 0.

(iv) True and false are non-numeric values.

Answer: (iv) True and false are non-numeric values.

Reason: True and false are keywords, they are non-numeric values which do not relate to zero or non-zero numbers. true and false are boolean values.

Que 38. Which of these packages is used for text formatting in Java programming language? [CBSE 2019 COMPT.]

(i) Java.text

(ii) Java.awt

(iii) Java.awt.text

(iv) Java.io

Answer: (i) java.text

You cannot copy content of this page

Scroll to Top