• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Tokens & Data Types

Java Tokens & Data Types

  • Categories Tokens & Data Types, Values & Data Types, Java
Java Tokens

Here we are going to learn above Java Tokens & Data Types. You can watch our video on Java Tokens & Data Types- Click Here

Q. 1 What is token? Give example of 2 types of token?
Ans. The smallest individual entity used in the program is known as java token. When we submit a Java program to the Java compiler, the compiler parses the text and extracts individual tokens
Types of token:

  1. Keyword
  2. Identifiers
  3. Literals
  4. Operators
  5. Separator (or Punctuators)

Q. 2 What are keywords? Give an example.
Ans. Keywords are reserved words which convey special meaning to the compiler. They are written in small letters. They cannot be used as names for identifiers, class or methods. E.g.: class, void

abstractdoubleintswitchcharforprotected
assertelseinterfacesynchronizedclassgotopublic
booleanextendslongthisconstifreturn
breakfalsenativethrowcontinueimplementsshort
bytefinalnewtransientdefaultimportstatic
casefinallypackagetruedoinstanceofsuper
catchfloatprivatetryvoidvolatilewhile

Q. 3 What are identifiers in Java?
Ans. Identifiers are tokens that represent names. These names can be assigned to variables, methods, and classes to uniquely identify them to the compiler. Variables are declared with a data type. E.g. n, x, val3, _age, $val etc.
A Java Identifier must have following characteristics

  • Can consist of uppercase, lowercase, digits, $ sign and underscore (_) character (NO SPACE)
  • Begins with letter, $, or _ (NO NUMBER)
  • Is case sensitive
  • Cannot be a keyword
  • Can be of any length

Q. 4 What are literals in Java?
Ans. Literals or constants are tokens which do not change its value during the execution of the program.
There are 5 types of Literals

  • Integer literal        
             E.g int WEEK = 7, color = 0x24, code = 0b0100, v = 10L;
  • Floating Point literal – It is of type double and float
             E.g. double d = 23.2, float PI = 3.14F;
  • Boolean literal – It has 2 values true and false
            E.g.  boolean STATE = true;
  • Character literal – It is always enclosed in single quotes
            E.g char ANS = ‘Y’, A = 065, ch = ‘\n’;
  • String literal – It is always enclosed in double quotes
            E.g String CAPITAL = “New Delhi”;

Q. 5 What is difference between float literal and double literal?
Ans.

Float Literaldouble Literal
The float literal is a single-precision 32-bit (4 bytes) floating point literal that can store any number along with fractional partThe double literal is a double-precision 64-bit (8 bytes) floating point literal that can store any number along with fractional part.
It can handle 7 decimal places.It can handle16 decimal places.

Q. 6 What do you mean by Punctuators? Give example?
Ans. Punctuators are also called as separators. They are specific symbols or tokens used to indicate how group of code is divided and arranged.
Examples:
          [ ]  (used for creating array),
          {}  (used to make compound statement body),
          ()  (used to perform arithmetic operations, used to enclose arguments of the function, for type casting),
           ;  (semicolon- used as statement terminator),
           ,  (comma- used to separate list of variables),
           .  (dot or decimal- used make fraction value, include package, referencing object).

Q. 7 What is the difference between Static and Dynamic Initialization?
Ans STATIC Initialization: Variable is initialized during its declaration with defined constant.
         E.g. int i = 10;
DYNAMIC Initialization: Variable is initialized at run time, i.e. during execution.
        E.g. i = a + b;

Q. 8 What is the final keyword used for?
Ans. The final keyword converts the variable into constant whose value cannot be changed at any stage in the program.
It is used when we need that the value of a variable should not change. Like value of constants , Pi etc.
Example:
        int x= 10;
        final int y = 20;
        x= x +7;
        y= y+7;     // it gives compilation error message-
                           //  “cannot assign a value to final variable y”

Q. 9 How much space do the following take – bit, terabyte, nibble,  kilobyte?
Ans.

NameSize
Bit1 bit
Nibble4 bits
Byte8 bits
Kilobyte1,024 bytes
Megabyte1,024 kilobytes
Gigabyte1,024 megabytes
Terabyte1,024 gigabytes
Petabyte1,024 terabytes
Exabyte1,024 petabytes
Zettabyte1,024 exabytes
Yottabyte1,024 zettabytes

Q. 10 What is Data type?
Ans. Data type refers to the type of data that can be stored in a variable. Java has two types Primitive and Reference Data Types.

Q. 11 What is Primitive Data Type?
Ans.
Primitive Data Types are Data types which are built into Java language. E.g. int,char etc. Java has following data type

Primitive Data Type

Q. 12 What is reference data type? Give 2 examples of reference data types?
Ans. The data type formed with the help of primitive data type are known as reference data type.
Example: Class, Interface and Array.

Q. 13 Why is Java called as strongly typed language?
Ans. Java is called as strongly typed language as variables can ONLY take value that matches the variable’s declared type. The compiler ensures that the program does not try to assign data of the wrong type to the variable.
For e.g. in statement below, you cannot assign double to an int variable. It will give an error.
      int val1 = 12.6; // Error

Q. 14 What is Type casting? What are 2 types of casting available in Java?
Ans.
To convert one predefined data type to another data type is known as Type Conversion or Type Casting.
There are two type of type casting:

  1. Explicit Type Casting
  2. Implicit Type Casting

Q. 15 What is Explicit and Implicit Type casting?
Ans.

Implicit Typecasting : 
    When a data type of lower size (occupying less memory) is assigned to a data type of higher size, it is done implicitly by the JVM. The lower size is widened to higher size. This is called as Implicit Typecasting and is also named as automatic type conversion.
Implicit type casting occurs if you assign any of these data types to higher data types
byte –> short –> int –> long –> float –> double
e.g
      int x = 10; // occupies 4 bytes
      double y = x; // occupies 8 bytes
      System.out.println(y); // prints 10.
Note A boolean value cannot be assigned to any other data type.

Explicit Typecasting : 
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size.
A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.
e.g.
    double x = 10.5; // 8 bytes
    int y = x; // 4 bytes ; raises compilation error
    int y = (int)x; // ok. Explicit Typecasting

  • Share:
author avatar
Simply Coding

Previous post

Conditional Statements in Java
June 16, 2021

Next post

Java Operators
June 16, 2021

You may also like

Questions on Encapsulation
Questions on Encapsulation
4 July, 2021
Questions on Library classes
Questions on Library classes
4 July, 2021
Questions on String 1
What is Java String?
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now