Programming Fundamentals.

Q1. Distinguish between a unary, a binary and a ternary operator. Give examples of Java operators for each one of them.

Q2. Given the following code fragment

int ch=20;

System.out.println(++ch);

System.out.println(ch);

  • What output does the above fragment produce?
  • What is the effect of replacing ++ch with ch+1?

Q3. What will be the rest of the following two expressions if i=10 initially?

i.) ++i<=10

ii.) i++<=10

Q4. Given the two following expressions:

a.) val=3          b.) val= =3

i.) How are these two different?

ii.) What will be the result of the if the values of val is 5 initially?

Q5. Construct an expression that is  equal to the absolute value of an variable. That is, if a variable p is positive, the value of the expression is just p, but if  p is negative, the value of the expression is -p, which would be positive. Do it in two ways:

i.) using a mathematical function

ii.) using a conditional operator.(Do not use the mathematical function here).

Q6. What output will the following code fragment produce?

int val, res, n=1000;

res = n + val > 1750 ? 400 : 200 ;

System.out.println(res);

i.) if the input is 2000     

ii.) if the input is 1000    

iii.) if the input is 500.

Q7. Given the following set of identifiers:

byte b;

char ch;

short sh;

int intval;

long longval;

float fl;

Identify the datatype of the following expressions:

a.)’a’-3     

b.) intval * longval – ch  

c.) fl + longval / sh

Q8. What is casting, when do we need it?

Q9. State the rules of operator precedence.

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.

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

Q12. One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?

Q13. What is a literal?

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

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s