Chapter 6

Q1.             Fill in the blanks

 

1.                   obj = new

2.                   util

3.                   input

4.                   nextInt()

5.                   Package

6.                   next()

7.                    /,*

8.                   import

9.                   Exception

10.               Syntax

Q2.             State true or false

 

1.               True

2.               True

3.               False

4.               True

5.               True

6.               True

7.               True

8.               False

9.               False

10.                  True

Q3.             

The process of assigning data or actual values to a declared variable is known as Initialization

 

For example-

int x=2; int y=4;

 

Q4.             

The disadvantage is that the user cannot understand the purpose of these input values because there is no message or indication for corresponding input.

Q5.  

The class that allows to input or read primitive data types is known as Scanner class. It is used to get input from inputstream.

 

Import java.util.*;

public class sample1

{

public void accept()

{

short num;

Scanner obj=new Scanner(System.in);

System.out.println("Input a number");

num=obj.nextShort();

System.out.println("The short integer number is= "+num);

 

}

}

Q6.

To perform proper input technique for the ease of the user is input stream method.

i.e. using Scanner class.

Q7.

The comments or remarks explain the process being used at one or more lines in the  programs. Java supports the following two comment or remarks symbols

  • // (pair of slash) - this symbol is used to write single line comment or remark in a program //any message or statement
  •  /*------*/ (Pair of slash and asterisk)

/*any message or statement----------

----------------------------------------------*/

Q8.

The unexpected situation which arises as run time error in the program is known as an exception or error. The errors are commonly classified into following three categories

1. Syntax errors or compile time errors

2. Run time errors

3. Logical errors


Q9.

The syntax errors that appear due to wrong use of syntax during the compilation of a program, while the runtime error appears during the execution of the program due to wrong evaluation, wrong division, wrong input data, etc...

Q10.

A program that compiles and runs successfully, but produces wrong output is known as logical error or semantic error.

Q11.

1. nextLong() - Returns the next token as a long OR It is used to input or read a long      integer from input device.

2. nextDouble()-Returns the next token as a double.It is used to input or read a double data from input device.

3. nextInt()-Returns the next token as an int(integer).It is used to input or read an integer data from input device.

4. next().charAt(0)-Returns only first character present at 0th index of any string. Eg. Let string is “He is fine”,then character char ch=next().charAt(0);returns ‘H’, means only the first letter.

5. nextShort()-Returns the next token as a short int(short integer).It is used to input or read a short integer data from input device.

6. next()-It returns next complete token as a string. It is used to input or read a string without any space from the input. If spaces are present then it reads only the first word or token and rest after space is truncated.

Q12.

i. bs.nextInt();

ii. bs.nextDouble();

iii. bs.nextFloat();

iv. bs.next().charAt(0);

Q13

i. Not a statement

ii. ‘;’ expected

iii. cannot find symbol-variable x

iv. ‘;’ expected

Q14.

SC.nextInt();

float y=SC.nextFloat();

Q15.

import java.util.*;

class Sample5

{

void cube()

{

int a;

Scanner s=new Scanner(System.in);

System.out.println("Enter the integer");

a=s.nextInt();

System.out.println("square of integer = " + (a * a) );

System.out.println("cube of integer =  " + (a * a * a) );

}

 }

Q16.

import java.util.Scanner;

class Rectangle

{

void area()

{

float length, breadth;

Scanner s1=new Scanner(System.in);

System.out.println("ENTER VALUES");

length=s1.nextFloat();

breadth=s1.nextFloat();

System.out.println("Area=" + (length * breadth) );

}

}

Q17.

import java.util.*;

class Convert

{

void change()

{

char ch1,ch2;

int temp;

Scanner s1=new Scanner(System.in);

System.out.println("Enter the Uppercase letter");

ch1=s1.next().charAt(0);

System.out.println("Original letter = " + ch1);

ch2 = (char) (ch1 + 32);

System.out.println("Lowercase letter = " + ch2);

}

}

Q18.

importjava.util.*;

class Number

{

void change()

{

long n1,n2;

Scanner s2=new Scanner(System.in);

System.out.println("Enter the values of A and B");

n1 = s2.nextInt();

n2 = s2.nextInt();

System.out.println("before swapping the values of A and B = " + n1 + " " + n2 );

n1 = n1+n2;

n2 = n1-n2;

n1 = n1-n2;

System.out.println("After swapping the values of A and B = " + n1 + " " + n2 );

}

}

Q19. import java.util.*;

class employee

{

public void salary()

{

double total,salary,hours,rate;

Scanner s=new Scanner(System.in);

System.out.println("enter salary");

salary=s.nextDouble();

System.out.println("enter the extra time worked");

hours=s.nextDouble();

System.out.println("enter the rate of extra 1 hr");

rate=s.nextDouble();

total=salary + (hours*rate);

System.out.println("total salary ="+total);

}

}

Q20 import java.util.*;

classStudent_Info

{

void Scores()

{

float e, m, p, c, b, ca;

Scanner s=new Scanner(System.in);

 

System.out.println("Enter english marks");

e = s.nextFloat();

System.out.println("Enter maths marks");

m = s.nextFloat();

System.out.println("Enter physics marks");

p = s.nextFloat();

System.out.println("Enter chemistry marks");

c = s.nextFloat();

 

System.out.println("Enter biology marks");

b = s.nextFloat();

System.out.println("Enter computer applications marks");

ca = s.nextFloat();

double tot=e + m + p + c + b + ca;

System.out.println("Total marks=" + tot);

double per = tot / 6;

System.out.println("Percentage" + per);

}

}

Q21 import java.util.*;

class Swap

{

void interchange()

{

char ch1,ch2;

Scanner s1=new Scanner(System.in);

System.out.println("Enter the characters");

ch1=s1.next().charAt(0);

ch2=s1.next().charAt(0);

System.out.println("The values before swapping are " + " " + ch1 +" and " + ch2);

char ch3 = ch1;

ch1=ch2;

ch2=ch3;

System.out.println("The values after swapping are" + " " + ch1 + " and " + ch2);

}

 }

Q22. import java.util.*;

class sample

{

void digits()

{

Scanner s1=new Scanner(System.in);

System.out.println("Enter the number");

long num=s1.nextInt();

System.out.println("Last two digits are " + num%100);

System.out.println("Last three digits are " + num%1000);

}

}

Q23. import java.util.*;

class item

{                 void get()

{

int pc, qty;

float price;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the product code");

pc=obj.nextInt();

System.out.println("Enter the product quantity");

qty=obj.nextInt();

System.out.println("Enter the product price");

price=obj.nextFloat();

float tot=qty*price;

float dis=(15*tot)/100;

System.out.println("product code=" + pc);

System.out.println("product quantity=" + qty);

System.out.println("product price=" + price);

System.out.println("total cost=" + tot);

System.out.println("discount=" + dis);

}

}

Q24. class sub

{

int subtract(int a)

{

int b = a % 10;

int f = a / 10;

int c = f % 10;

int d = b + c;

return(d);

}

void display()

{

int d = subtract(3457);

System.out.println(d);

}

}

Q25. import java.util.*;

class sample4
{

void Square()

{

Scanner s1=new Scanner(System.in);

int a;

System.out.println("Enter the value");

a=s1.nextInt();

int area=a*a;

System.out.println("Area="+area);

}

float Interest()

{

System.out.println("Enter the values");

System.out.println("Enter principal amount: ");

int pa = s1.nextInt();

System.out.println("Enter time");

int t = s1.nextInt();

System.out.println("Enter i ");

float i = s1.nextFloat();

float si = (pa * t * i)/100;

return(si);

}

void display()

{

Square();

float ans = Interest();

System.out.println("Simple Interest="+ans);

}

}

Q26.

import java.util.*;

class circle

{

void area()

{

Scanner s1=new Scanner(System.in);

float r;

final float pi=3.14f;

System.out.println("Enter the radius");

r = s1.nextFloat();

float a=r * r * pi;

System.out.println("Area of Circle = " + a);

}

}

Q27. import java.util.*;

class employee

{

public void salary()

{

double empcode, bpay, da, hra, pf, gs,netsal;

Scanner s = new Scanner(System.in);

System.out.println("Enter the empcode and basicpay");

empcode = s.nextDouble();

bpay = s.nextDouble();

da = (65 * bpay) / 100;

hra = (21 * bpay) / 100;

pf = (8.33 * bpay) / 100;

gs = da + hra + bpay;

netsal = gs - pf;

System.out.println(" DETAILS ARE ");

System.out.println("______________________");

System.out.println("EMPLOYEE CODE : " + empcode);

System.out.println("BASIC SALARY : " + bpay);

System.out.println("DAILY ALLOWANCE : " + da);

System.out.println("HOUSE RENT ALLOWANCE: " + hra);

System.out.println("GROSS SALARY : " + gs);

System.out.println("NET SALARY : " + netsal);

}

}

Q28.

i. Int ne w=347.0; ------------------------- int new=347.0;

 

ii. chars var$=M; ------------------------- char var$=’M’;

iii. Double jk==87; ------------------------- double jk=87;

 

iv. void public print(int y, float d) ----------------- public void print(int y, float d)

v. void Demo(float y, d ,Long ch, Char h) ----- void Demo(float y, float d ,Long ch, Char h)

vi. scanner obj=new Scanner(system.in); - Scanner obj=new Scanner(System.in);

vii. int x = sr.nextShort() ----------------------- int x = sr.nextInt();