0% found this document useful (0 votes)
19 views5 pages

Programming HW

programmming answers

Uploaded by

sthaujwal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Programming HW

programmming answers

Uploaded by

sthaujwal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 -> public class Swap{

public static void main(String[] args){


int a=5;
int b=10;
int c;
//Before
System.out.println("Before Swapping");
System.out.println("a = "+a);
System.out.println("b = "+b);
//After
c=a;
a=b;
b=c;
System.out.println("After Swapping");
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}

2 -> public class Swap2{


public static void main(String[] args){
int a=5;
int b=10;
//Before
System.out.println("Before Swapping");
System.out.println("a = "+a);
System.out.println("b = "+b);
//After
a=a+b;
b=a-b;
a=a-b;
System.out.println("After Swapping");
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}

3 -> The different types of comments used in JAVA programming are:


1) // ( used to comment in single line where the Java will ignore text after // .)
2) /* */ ( It is a multi-line comment used to comment in more than one line where the
code between /* and */ is ignored by Java.)
3) /** */ ( used to provide information about the software’s producer, what the
producer does, how to use it, and its version.)

4 -> The different types of data types used in JAVA programming are:

A. Primitive data types


It is the most basic data type and it represents simple value(numbers, booleans or
characters). They hold their data directly in memory. It has 8 primitive data type:

 Byte
 Int
 Short
 Long
 Float
 Double
 Char
 Boolean

B. Reference data types


It is used to store references to objects. It is used while dealing with complex data
structures. They store the reference to the data rather than data itself. It data types
are:

1. Objects
2. Arrays
3. Interfaces
4. Enumerations (Enums).

5 -> The different types of operators used in JAVA programming are:

1) Arithmetic Operator
2) Relational Operator
3) Logical Operator
4) Assignment Operator
5) Bitwise Operator
6) Ternary/Conditional Operator
6 -> public class Converter{
public static void main(String[] args){
float a=12; //Celsius is denoted by a
float convert;
convert = ((a*9)/5)+32; //Converting Celsius into Fahrenheit
System.out.println("Temperature in Fahrenheit is: "+convert+"F");
}
}

7 -> The purpose of System.out.println() method is to display output of the given


program by User and to add new line after it.

8 -> The use of “=” is to assign value to a variable whereas “==” is used to compare any
two values.

9 -> The syntax to declare the variable is given below:

type variableName = value;

10 -> Before Correction


pubic class MyClass {
Public void min(String[] arguments{
A = 10;
String name = “Test”;
int total = name + 10;
System.out.println(Test);
System.out.print(tot);
}
}
After Correction
public class MyClass {
public static void main(String[] args) {
int a = 10;
String name = “Test” ;
String total = name + a;
System.out.println(name);
System.out.println(total);
}
}

11 -> Before Correction


public class Addition.{
public static void main(Args[] string){
system.Out.printline(“Printing…””);
int = 10;
int = 20;
c = a + b;
system.Out.printline(c);
}
}

After Correction
public class Addition{
public static void main(String[] args){
System.out.println("Printing.....");
int a = 10;
int b = 20;
int c=a+b;
System.out.println(c);
}
}

12 ->
a) char c = ‘a’;
-> Legal because char datatype is used for single characters only and the value ‘a’ is
a valid character.

b) int i = c+10;
-> Legal because we can add value of a variable.

c) String name = “Pawan”;


-> Legal because string must be enclosed with double quotes(“..”).

d) String char = “Darwin”;


-> Illegal because char is used for single characters only and here “Darwin” is a
string.
e) String place = ‘Kathmandu’;
-> Illegal because single quotes(‘..’) are meant for only char, not for string. For string
double quotes(“..”) are valid.

f) int 1 =5;
-> Illegal because variables cannot start with a number. It can only be start with a
letter,_,$.

g) String five = 3;
-> Illegal because variable five is a string and it can only hold characters not an
integer value.

h) String 1name = “Harry”;


-> Illegal because the variable cannot start with a digit or number but here the
variable starts with a 1 .

i) char b = ‘45’;
-> Illegal because char data type only accepts a single character like ‘4’.

j) int num = five;


-> Illegal because five is a string not a number and we cannot directly assign string
to an int.

k) int salary = 10,000;


-> Illegal because java does not allow commas in numerical values.

l) String _email = “[email protected]”;


-> Legal statement because it starts with an underscore(_) which is valid and also
the string value is enclosed with double quotes so it is valid.

You might also like