Code Fragments.

Q3. Consider the following two code fragments for counting spaces and newlines:

//version 1

:

if ( ch= = ‘  ‘ )

spaces + + ;

if ( ch = = ‘ \ ‘ )

newlines + + ,

//version 2

:

if ( ch= = ‘  ‘ )

spaces + + ;

else if ( ch = = ‘ \n ‘ )

newlines + + ;

What advantages, if any, does the second version have over the first?

Ans. Version 1 uses two separate if statements. Thus, two times the conditions are tested whereas the Version 2 uses an if-else construct. If the first condition is true in Version 2, the second condition is never tested in contrast to Version 1 where both conditions are always tested irrespective of whether the first condition is true or false.

Therefore, the Version 1 take more processing time as compared to Version 2 ( in case the first condition is true). The Version 2 is more efficient compared to Version 1.

Advertisements

Fall Through.

Q2. What is a “fall through”?

Ans. The term “fall through” refers to the way the switch statement executes its various case sections. Every statement that follows the selected case section will be executed unless a break statement is encountered.

Theory Questions.

Flow Of Control – Theory Questions.

Q1. How is the if..else if combination more general than a switch statement?

Or

Q1. Difference between if..else if combination and a switch statement?

Q2. What is a “fall through”?

Q3. Consider the following two code fragments for counting spaces and newlines:

//version 1

:

if ( ch= = ‘  ‘ )

        spaces + + ;

if ( ch = = ‘ \ ‘ )

        newlines + + ,

//version 2

:

if ( ch= = ‘  ‘ )

        spaces + + ;

else if ( ch = = ‘ \n ‘ )

        newlines + + ;

What advantages, if any, does the second version have over the first?

Difference if..else n switch.

Q1. How is the if..else if combination more general than a switch statement?

Or

Q1. Difference between if..else if combination and a switch statement?

Ans. The switch statement must be controlled by a single integer control variable, and each case section must correspond to a single constant value for the variable.

The if.. else if combination allows any kind of condition after each if.  

 

Programming Questions.

Flow Of Control – Programming Questions.

Q1. Given the value of a variable, write a statement, without using if construct, which will produce the absolute value of the variable.

Ans. x = ( x < 0 ) ? -x : x ;

Q2. What is wrong with the following code?

switch(x)         {

case 1 :

        n1=10;

        n2=20;

case 2 :

        n3=30;

        break;

        n4=40;

}

Ans. n=40; is unreachable because of break statement above it.

Q3. What is the problem with the following snippet?

:

int i = 5 , j = 10 ;

if ( ( i < j ) | | ( i = 10 ) )

System.out.println(“OK”);

System.out.println(“NOT OK”);

Ans. (i=10) used with if is the problem.

Q4.Show the output of the following code:

int a = 5 , b = 10 ;

if ( a > 5 )

        if ( b > 5 ) {

                System.out.println(“b is “+b);

        }

else

                System.out.println(“a is “+a);

Ans. a is 5.

Q5. Show the output of the following code:

int a = 10, b = 5;

if ( a > b){

        if ( b > 5 )

                System.out.println(“b is “+b);

        }

else

              System.out.println(“a is “+a);

Ans. No output.

Q6. What will the output of following two code fragments:

//version1

i = 10 ;

j = 10 ;

if ( a < 100 )

if ( b > 50 )

+ + i ;

else

+ + j ;

System.out.println( ” i = ” + i ) ;

System.out.println( ” j = ” + j ) ;

//version2

i = 10 ;

j = 10 ;

if ( a < 100 ) {

if ( b > 50 )

+ + i ;

}

else

+ + j ;

System.out.println( ” i = ” + i ) ;

System.out.println( ” j = ” + j ) ;

if the input given is shown below:

(i) a = 30 , b = 30

(ii) a = 60 , b = 70

Ans. case (i) when a = 30 , b = 30

version 1’s output will be as follows:

i=10

j=11

version 2’s output will be as follows:

i=10

j=10

case (ii) when a = 60 , b = 70

version 1’s output will be as follows:

i=11

j=10

version 2’s output will be as follows:

i=11

j=10

Q7. Identify the error(s) in the following code fragment:

switch(ch){

case ‘a’:

case ‘A’:

case ‘e’:

case ‘E’:

case ‘i’:

case ‘i’:

case ‘o’:

case ‘o’:

case ‘u’:

case ‘U’: ++vowels;

break;

default:++others;

}

:

Ans. The errors in the above code fragment are:

  1. An uninitialized variable ch is being used in while’s test expression. The variable ch must have a value before it is used in an expression.
  2. The switch statement’s two case constants are identical: case ‘i’ and case ‘i’. The case constants of same switch have different values.

Q8. What will be the output of following code fragment if the value of ch is

(i) a

(ii) c

(iii) d               

(iv) h        

(v) b?

:

switch (ch){

                        case ‘a’ : System.out.println(“It is a.”);

                        case ‘b’ : System.out.println(“It is b.”);

                        case ‘c’ : System.out.println(“It is c.”);

                        break;

                        case ‘d’ : System.out.println(“It is d.”);

                        break;

                        default : System.out.println(“Not a b c d.”);

                }

                :

Ans.  (i) When input is a, the output will be as follows:

It is a.

It is b.

It is c.

(ii) When input is c, the output will be as follows:

It is c.

(iii) When input is d, the output will be as follows:

It is d.

(iv) When input is h, the output will be as follows:

Not a b c d.

(v) When input is b, the output will be as follows:

It is b.

It is c.

Q9. Give the output of the following code:

int m =100;

while(m>0){   //true is boolean value

if (m<10)

break;

m=m-10;

}

System.out.println(“m is “+m);

Ans. m is 0.

Q10. Give the output of the following code:

int m=100;

while(true){

if (m<10)

continue

m=m-10;

}

System.out.println(“m is “+m);

Ans. No output; Infinite loop.

A Variable Declaration Statement.

Q14. What does the computer do when it executes a variable declaration statement? Give an example.

Ans. A variable is a box, or location, in the computer’s memory that has name. The box holds a values of some specified type. A variable declaration statement is a statement such as

int x;

which creates the variable x. When the computer excutes a variable declaration, it creates the box in memory and associates a name (in this case, x) with that box. Later in the program, that variable can be referred to by name.

 

Literal.

Q13. What is a literal?

Ans. A literal is a sequence of characters used in program to represent a constant value. For example, ‘A’ is a literal that represents the value A, of type char, and 17L is a literal that represents the number 17 as a value of type long. A literal is a way of writing a value, and should not be confused with the value itself.

Type, Relation To Programming.

Q11. What is a type, as this term relates to programming?

Ans. A type or datatype represents a set of possible values. When we specify that a variable has a certain type, we are saying what values it can hold.When we say that an expression is of a certain type, you are saying what values the expression can have. For example, to say that a variable is of type int says that integer values in a certain range can be stored in that variable.

Java Statement.

Q10. Suppose x1 and x2 are two double type variables that you want to add as integers and assign to an integer variable. Construct a Java statement for doing so.

Ans. Assuming that target variables is res of type int.

res = ( int ) ( x1 + x2 ) ;