Expressions and Statements

An expression in Java is any valid combination of operators, constants and variables, i.e., a legal combination of Java tokens. The type of operators used in an expression determines the expression type. Arithmetic expression uses arithmetic operators. Arithmetic expressions may contain variables, constants and arithmetic operators together to produce a meaningful result. Boolean expression uses Boolean operators. Boolean expressions involve logical and relational operators and result into ‘true’ or ‘false’. When constants and variables of different types are mixed in an expression, they are converted to the same type. There are two types of conversions: Implicit Type Conversion and Explicit Type Conversion. Implicit type conversion is performed by compiler. Java compiler converts all operands up to the type of largest operand called type promotion. This process of conversion wherein data types are promoted is known as coercion. Explicit type conversion is performed by the programmer using (type) operator. This process is known as type casting.

A Java statement generally contains expressions. The statements must end with a semi-colon. The various types of Java statements are: Expression Statements, Declaration Statements, Control Flow Statements and Empty/Null Statement. Expressions that are made into statements by terminating with semicolon (;) are called expression statements. Declaration statements are used to declare variables. Control Flow statements regulate the order in which statements get executed. Null statement is a semicolon (;) only. It is used when syntax demands a statement but the logic does not. A block is a group of zero or more statements between curly braces and can be used anywhere in a program.

To Access the full content, Please Purchase

  • Q1

    Write a program using ‘if-else’ statement to print number having maximum value out of two given values.

    Marks:15
    Answer:

    public class maximumVal

    {

    public static void main( String args [ ])

    {

    int a = 12;

    int b = 10;

    if (a>b)

    {

    System.out.println(" a has maximum value " );

    }

    else

    {

    System.out.println(" b has maximum value " );

    }

    }

    }

    Output:

    View Answer
  • Q2

    (a) Write a program in Java to find the sum of the series.

    S = 1+(1+2)+(1+2+3)+……………10 terms.

    Illustrate the working of ‘break’ statement.

    Marks:15
    Answer:

    class series

    {

    Public static void main(String args[ ])

    {

    int S=0;

    for (int c=1;c<=10;c++)

    {for (int d=1;d<=c;d++)

    {

    S=S+d;

    }

    }

    System.out.println("Sum of Series 1+(1+2)+(1+2+3)+ --------+(1+2+3--------+10) is :" +S);

    }

    }

    (a) Consider a situation where you are looping from 1 to 5 and you want to find the sum of numbers 1, 4 and 5, except 2 and 3. So, when you reach 2 and 3, you will have to skip the loop. Here, we use the ‘break’ statement.

    sum=0;
    for(i=1;i <=5;i++)
    {
    if (i==2 || i==3)
    break;
    sum = sum+i;
    }

    Now, you have your answer 10.


    In short, the ‘break’ statement helps you to skip a loop and then continue with the rest of the iteration.

    View Answer
  • Q3

    (a) Classify access specifiers with their access. [2]

    (b) How class variables are made? Explain with example. [2]

    (c) What are constants? [2]

    (d) What are non-primitive data types? [2]

    (e) What is the alternative of if…else statement?[2]

    (f) What are the different types of views in PowerPoint presentation? [2]

    (g) Explain default constructor and parameterised constructor. [2]

    (h) Differentiate between while loop and do-while loop. [2]

    (i) State the value of n and ch when [2] char c=’A’;

    int n=c+1;

    char ch=(char)n;

    (j) What are the different types of tokens? [2]

    Marks:20
    Answer:

    (a) Java provides four access specifiers:

    Access Specifiers

    Same Class

    Same Package

    Subclass

    Other packages

    public

    Y

    Y

    Y

    Y

    protected

    Y

    Y

    Y

    N

    no access modifier(default)

    Y

    Y

    N

    N

    private

    Y

    N

    N

    N

    (b) Class variables are made by declaring variable with ‘static’ keyword.

    Example:

    class xyz

    {

    int a; // instance variable

    static int b; // class variable

    }

    (c) A constant is a variable that does not change its value during the execution of a program. Example: char ch =‘s’, here the value of ch will remain the same in the program. The different types of constants used in Java program are: integer, real, character and string constant.

    (d) Non Primitive Data Types are directly or indirectly dependent on primitive data types. They are also known as derived data types like arrays, structure etc.

    (e) The conditional operator (? :) can be used as an alternative to if-else statement. Compared to if-else sequence, conditional operator (? :) offers more concise, clean and compact code.

    (f) The different types of views in PowerPoint presentation are:
    1. Normal view
    2. Outline view
    3. Slide view
    4. Slide Sorter view
    5. Slide show
    6. Notes page

    (g) A constructor which initializes instant variable of an object with definite values readily defined is known as default.

    A parameterized constructor is a member function with same name as the class name which is used to initialize the parametric values at the time of its creation.

    (h)

    While loop

    Do-while loop

    The while loop will not execute at all if the condition is not satisfied.

    The do….while loop will continue at least once even if the condition is not satisfied.

    while loop checks the condition first and then execution begins.

    The do….while loop checks the condition after executing the loop once.

    It is an entry controlled loop.

    It is an exit controlled loop.

    (i) The value of:
    n=66
    ch=B

    (j) Different types of tokens available in java are:Literals, Operators, Identifiers, Punctuators, Separators and Assignment.

    View Answer
  • Q4

    (a) Explain specifiers or access or visibility rules. [2]
    (b)
    Differentiate Shift Left and Shift Right operations. [2]
    (c)
    What are the values of x and y when the following statements are executed? [2]
    int a=52, b=38;
    boolean x=(a>b)? a:b;
    int y=(a

    Marks:10
    Answer:

    (a) Access or visibility rules determine whether a method or a data variable can be accessed by another method in another class or subclass.

    (b) The Shift Left (<<) operator shifts the bit pattern of the operand towards left by defining number of bits whereas, Shift Right (>>) shifts the bit pattern of the operand towards right by defining number of bits.

    (c)The statement given below is wrong:

    boolean x=(a>b)?a:b;

    Should be:

    boolean x=(a>b)?true : false; then,

    x=true

    y=38

    (d) Relational operators are operators used to show the relationship among the operands. Relational operators compare the values of the variables and result in ‘True’ or ‘False’ (i.e., 0 or 1). The different types of relational operators are:

    Symbol

    Meaning

    <

    Less than

    >

    Greater than

    <=

    Less than or equal to

    >=

    Greater than or equal to

    ==

    Equal to

    !=

    Not equal to

    (e) The two principles of Object Oriented Programming are:

    (1) Encapsulation: It is a system of wrapping data and functions of a class as a unit.

    (2) Data Abstraction: It is an act of representing essential features of a class without knowing its background details.

    View Answer
  • Q5

    (a) Why do we use Header and Footer in a PowerPoint presentation? [2]
    (b)
    What is Slide Sorter view used for? [2]
    (c)
    Explain Cascading Delete. [2]
    (d)
    How will you open a report in Design view? [2]
    (e) How do you choose the right loop for a specific job? [2]

    Marks:10
    Answer:

    (a) A header or footer appears at the top or bottom of each slide in the presentation. Header and Footer are used to display recurring information on each slide and to keep track of the content.

    (b) A Slide Sorter view displays the rows of thumb nail views of the slides. The Slide Sorter View makes it easy to reorganize, insert, delete and copy the slides.

    (c) A Cascading Deleterefers to the deletion of all the related records in the related table or tables when a record in the primary table is deleted. This can be done for relationships that enforce referential integrity between tables.

    (d) Report in Design view can be opened in two ways:

    1. Click Report tab on the database window and then click Design button on the database toolbar.

    2. Click Report tab on the database window and then click at Create Report in Design view.

    (e) Whenever we require fixed iterations, we make use of the ‘for’ loop and if the number of iterations are unknown, then we make use of the ‘while’ loop. However, if we want to perform at least one iteration, we make use of the ‘do…while’ loop.

    View Answer