Practicals 1
Practicals 1
Practicals 1
Uttara InfoSolutions
www.uttarainfo.com
Practicals 1:
Instructions:
a) Install JDK, TextPad in your laptop by using help of Lab instructors. Set up the path
environment variable in Windows (Control Panel -> System -> Adv Settings ->
Environment variables -> System Env Variables -> select path -> click edit -> add ; to end
and copy paste path till bin folder of JDK). Test whether javac.exe and java.exe work in
command prompt (Start->cmd in windows).
b) Create a directory (ideally in c:\) with pgms (or with your name, do not have space in the
folder name please). Create 3 sub folders, source, classes and lib within it.
c) Open two command prompts (windows start-> cmd two times).
In one, type in cd c:\pgms\source (enter) and cd c:\pgms\classes (enter).
b) Create a A.java. Create 3 class definitions named A, B, C. Try to make all of them be
marked public. Have empty body. Compile A.java. Are you able to compile? If not why not?
Mark B, C with package access (remove access specifier) and then compile. Now is it
compiling? Why so?
Insert main() in all 3 call definitions with different SOPs. Which main() will gets executed?
Execute all the 3 class`s main().
c) Try to change the header of the main() -> remove String[], change it to int[], make main()
protected, etc and try to compile and run. What fails and why?
d) Create an int variable in main() named i. Assign value 5 to it and print it out using SOP.
Compile and test. Next do not assign any value to i and try to recompile. Do you get any
error? why? what does it say?
byte b = 100;
Try to assign 128 to a byte. Are you able to compile? what about -128? Try to compile, run.
f) Create a byte variable, assign it a value and then store in an int variable.
byte b = 50;
int x = b; // why does this work?
b = x; // why does this not work?
g) Replace last statement with b = (byte) x; and see if it works. Now test the same by
assigning a value to a short, a char and try to assign to an int, long, float, double. Also from
the bigger datatyped variables, put the value to smaller data typed variables. Do not create
2 variables with the same identifier.
h) Try out all the arithmetic operators and print out the results.
int x = 10;
int y = 4;
int result1 = x + y;
int result2 = x * y;
int result3 = x / y;
int result4 = x % y;
int result5 = x - y;
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
System.out.println(result5);
a) System.out.println("1"+2+3);
b) System.out.println(6+2+"4");
c) System.out.println('2'+2+"2");
d) System.out.println(‘2'+2);
a) byte b = 10;
b = b + 10;
System.out.println(b);
b) byte b = 10;
b+=10;
System.out.println(b);
a) char c = 'X';
int a = 63;
char d = a;
System.out.println(d);
b) char c = 'X';
int a = 63;
char d = (char)a;
System.out.println(d);
m) Add 100 + 100 and store result in a byte. Print the value. Do you understand why is the
result the one being printed?
Other programs:
a) System.out.println(10);
b) System.out.println(012);
c) System.out.println(0xAB);
Check on paper whether you are getting the correct result.
15) Execute the code given below and check the o/p ?
16) Execute the code given below and check the o/p ?
a) int x = 10;
System.out.println(~x);
b) int a = 100;
System.out.println(~a);
Check on paper whether you are getting the correct result.
17) Execute the code given below and check the o/p ?
a) int x = 10;
int y = 6;
System.out.println(x | y);
b) int a = 100;
int b = 50;
System.out.println(a | b);
Check on paper whether you are getting the correct result.
a) int x = 10;
int y = 6;
System.out.println(x & y);
b) int a = 100;
int b = 50;
System.out.println(a & b);
Check on paper whether you are getting the correct result.
19) Execute the code given below and check the o/p ?
a) int x = 10;
int y = 6;
System.out.println(x ^ y);
b) int a = 100;
int b = 50;
System.out.println(a ^ b);
Check on paper whether you are getting the correct result.
20) Execute the code given below and check the o/p ?
a) int x = 10;
System.out.println(x >>> 2);
b) int a = -10;
System.out.println(a >>> 2);
Check on paper whether you are getting the correct result.
22) Execute the code given below and check the o/p ?
a) System.out.println(4 | 3);
b) System.out.println(4 | 4);
c) System.out.println(4 & 3);
d) System.out.println(4 / 3);
Check on paper whether you are getting the correct result.
23) Execute the code given below and check the o/p ?
int i = 50;
while(i != 0){
System.out.println(i >>>= 1);
}
24) Create a class Test. Create a method named process that accepts an int as a
parameter. Create a main method. Create a int variable x with value 10. Print the value of
x to monitor using SOP. Then invoke process() and pass x as a parameter. Print the value
in process(). Change the value in process(). Print the value in main() after invoking
process(). Are you understanding the control flow? Are you understanding how parameters
are being passed?
26) Test main() calling another static method x() which should call another static method
y() in the same class. Put SOPs in each method to understand flow of control. How does
the JVM manage control flow? What data structure does it internally use? Create local
variables in each method. Try to access one methods local variable in another method.
Can you access it? Why not? Where are local variables stored in memory? How do you
explain parameter passing? Now create another class with name Test2 and create a static
method named z() in it. Call this method from Test`s main() and see how control flow
occurs.
27) Put ‘a’ into a char. Put this variable into an int variable. Print out the value. Why do you
the value you see being printed? Now print the next 25 values in a loop and print out the
symbols by incrementing the char variable.
}
3)
while(i > 10)
{
}
4)
do
{
}
while(i <=10);