SET- 1
1.Use the echo command to display the line
UNIX is fun to learn
Redirect the displayed line to a file. Append the outputs of the commands to show the users logged into your system, your login account and the current date in the dd-mm-yyyy format to the file.
Answer-
echo ”UNIX is fun to learn”
script –a myfile who –a whoami date +%d-%m-%Y
----------------
2.Write a program to print “ Welcome to Java Programming” on the screen.
Answer-
class welcome
{
public static void main(String a[])
{
System.out.println("Welcome to Java Progamming");
}
}
----------------
SET- 2
1. Assuming you have a directory containing the following files:
sprite, cola, choco, orange, book, lemon, lotus, apple
use the ls command to list only those files that
Answer-
a) consist of four letters
Answer- ls ????
b) begin with the letter c
Answer- c*.*
c) end with the letter e
Answer- *e.*
d) have the second letter o
Answer-?o.*
----------------
2.Write a program to check whether the given two numbers are equal. If not then find the greater of the given two numbers.
Answer-
import java.lang.*;
class Comparision
{
public static void main(String args[])
{
int x,y;
x=67;
y=97;
if(x==y)
{
System.out.println("Given two number are equal");
}
else if(x>y)
{
System.out.println(x+ " is greater than " +y);
}
else
{
System.out.println(y+ " is greater than " +x);
}
}
}
----------------
SET -3
1.For a file named myfile in the working directory, do the following:
a) give everyone permission to read myfile. No other privilege is to be changed
Ans :- chmod a+r myfile
b) allow the owner and group members to read and write the file. All privileges are to be removed for everyone else.
Ans:- chmod 660 myfile
c) remove write privileges from everyone except the owner
Ans:- chmod go-w myfile
d) allow the owner and group members to execute myfile and give only the owner permission to read or write to it.
Ans:- chmod 710 myfile , chmod ug=x,u+rw myfile
----------------
2.Using switch and case statements write a program to add, subtract , multiply and divide the two numbers.
Answer-
import java.io.*;
public class BasicCalc
{
public static void main(String a[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("What do you want to do? \n 1. Add, \n 2. Substract, \n 3. Multiply, \n 4. Divide");
int i = Integer.parseInt(in.readLine());
//int i=4;
int m, n;
m = 10;
n = 5;
switch (i)
{
case 1:
System.out.println("Result after Adding" + " " + (m + n));
break;
case 2:
System.out.println("Result after Substracting" + " " + (m - n));
break;
case 3:
System.out.println("Result after Multiplying" + " " + (m * n));
break;
case 4:
System.out.println("Result after Dividing" + " " + (m / n));
break;
default:
System.out.println("This is not a valid input parameter");
break;
}
}
}
----------------
SET -4
1. For a directory myfolder in your working directory, do the following:
a) allow everyone to list files in myfolder. No other privileges are to be changed.
Ans:- chmod a+rx myfolder
b) allow the owner to and group members to list, remove or add files. All privileges are to be removed from everyone else.
Ans:-chmod 770 myfolder
c) give write privileges to everyone except the owner
Ans:- chmod go+w myfolder
d) allow the owner and group members to execute myfolder. Only the owner gets read or write permission.
Ans:- chmod 710 my folder
----------------
2.Using for loop write a program to print the table of a given number on the screen.
Answer-
import java.util.Scanner;
public class Table
{
public static void main(String args[])
{
int num,i,res;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
num =s.nextInt();
for(i=1; i<=10; i++)
{
res=i*num;
System.out.println(res);
}
}
}
----------------
SET -5
1.Put a long-running process in the background and check the accuracy of the sleep command.
Answer-
date;sleep10;date$
----------------
2.Write a program to find the even sum and the odd sum in the given array.
Answer-
class EvenOddSum
{
static int sum1, sum2;
static int n[] ={ 2, 3, 4, 5, 6, 7, 92, 65, 45, 87 };
public static void main(String a[])
{
for (int i = 0; i <=n; i++)
{
if (n[i] % 2 == 0)
{
System.out.println(n[i] + " " + "*" + " " + "Even");
sum1 = sum1 + n[i];
}
else
{
System.out.println(n[i] + " " + "*" + " " + "Odd");
sum2 = sum2 + n[i];
}
}
System.out.println("sum of even no. is " + sum1);
System.out.println("sum of odd no. is " + sum2);
}
}
----------------
SET -6
1.The ls command with the –R option lists the files in the specified directory and also subdirectories, if any. Use this with a suitable pip-and-filter arrangement to determine whether a file exists in the account logged in.
Answer-
echo “Enter the name to search :”
read file
dat =’ls-R $ Home 1 grep $ file’
if [-s “$ dat”+
then
echo “$file File exist”
else
echo “$file File not exist”
fi
----------------
2.Write a program to print all the prime numbers between n and m.
Answer-
import java.io.*;
class PrimeNumber
{
public static void main(String[] args) throws Exception
{
int i;
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter 1st number: ");
int num = Integer.parseInt(bf.readLine());
System.out.println("Enter 2nd number: ");
int num2 = Integer.parseInt(bf.readLine());
System.out.println("Prime numbers are: ");
for (i = num; i <num2; i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i % j;
if (n == 0)
{
break;
}
}
if (i == j)
{
System.out.print(" " + i);
}
}
}
}
----------------
SET- 7
1.Write an interactive shell using I/O redirection to accept input from file1, put stdout in file2 and stderr in file3.
Answer-
echo “Enter input file name:”
read f1
echo “Enter output file name:”
read f2
echo “Enter error file name:”
read f3
cat <$f1 , cat >$f2 , cat >$f3
----------------
2.Write a program to add the two matrices.
Answere-
class MatrixSum
{
public static void main(String[] args)
{
int array[][]= {{4,5,6},{6,8,9}};
int array1[][]= {{5,4,6},{5,6,7}};
System.out.println("Number of Row = " + array.length);
System.out.println("Number of Column = " + array[1].length);
int l= array.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++)
{
for(int j = 0; j <= l; j++)
{
System.out.print(" "+ array[i][j]);
}
System.out.println();
}
int m= array1.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+array1[i][j]);
}
System.out.println();
}
System.out.println("Addition of both matrix : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+(array[i][j]+array1[i][j]));
}
System.out.println();
}
}
}
----------------
SET- 8
1.Write a shell program to extract and sort all the users in the file system. Use the /etc/passwd file as input.
Answer-
first (with pipe and filter)
Cut –d ”:” –f1/etc/passwd | sort
Second (without pipe and filter)
Cut –d “:” –f1/etc/passwd>tmp sort<temp
----------------
2.Write a class circle which consists of functions getdata() and area(), and also write a main program to create a circle object and to find the area by calling the function area () in circle class and display the result on the screen.
Answer-
import java.lang.*;
import java.io.*;
class Circle
{
double r,a;
static final double p=3.14;
public void getData()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of the circle :");
r=Double.parseDouble(br.readLine());
}
catch(Exception e)
{
}
}
public void area()
{
a=p*(r*r);
System.out.print("The area of the circle is :"+a);
}
public static void main(String args[])
{
Circle c=new Circle();
c.getData();
c.area();
}
}
----------------
SET- 9
1.Write a shell program to translate the /etc/passwd file into uppercase and translate the “:” delimiter into tab characters.
Answer-
tr ”*a-z:+” “*A-Z:+” </etc/passwd
----------------
2.Write a program to demonstrate the method overloading for sum ( ) function.
Answer-
class Demo
{
void add(int a)
{
System.out.println(a);
}
void add(int x, int y)
{
System.out.println(x + y);
}
public static void main(String args[])
{
demo q = new demo();
q.add(10);
q.add(20, 30);
}
}
----------------
SET- 10
1.Write a shell program using if-the-else to test whether a variable name is a directory or a file.
Answer-
echo “Enter a name”
read name
if [-f $name]
then
echo “$name is a file”
then
echo “$name is a directory”
else
echo “$name is not a file nor a directory.”
fi
----------------
2.Write a program to concatenate two strings.
Answer-
class Conct
{
public static void main(String a[])
{
String a1 = "Vikash";
String a2 = "Kumar";
System.out.println(a1.concat(" " + a2));
}
}
----------------
SET- 11
1.Write a shell program in which an infinite loop prompts a user for file names to be removed and remove the files. Use trap to exit when finished.
Answer-
trap ‘exit’ INT
While [true]
do
echo “Enter filename to be deleted (ctr+c to exit)”
read file
rm $file
done
----------------
2.Write a program to find whether a given string is a palindrome or not.
Answer-
class Palin
{
public static void main(String []s)
{
int i=0,j;
j=s[0].length()-1;
while(i<j)
{
if(s[0].charAt(i) != s[0].charAt(j))
break;
i++;
j--;
}
if(i<j)
System.out.println("Not palin");
else
System.out.println("Palin");
}
}
----------------
SET- 12
1. Write a background command to edit all the files in the directory replacing the word while with until.
Answer-
tr 'while' 'until' < myfile &
----------------
2. Write a program to change the case of the given string.
Answer-
import java.io.*;
class case
{
public static void main(String ar[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter any String");
System.out.println("and to quit write “ Exit“ ");
System.out.println("\n");
do
{
str = br.readLine();
int textLength = str.length();
for (int i = 0; i < str.length; i++)
{
char ch = Character.toUpperCase(str.charAt(i));
System.out.print(ch);
}
} while (!str.equals("exit"));
}
}
----------------
SET- 13
1.Write a shell program to test for arguments arg1, arg2 and arg3. If they are not present, prompt the user for them.
Answer-
if [$ # -lt 3]
then
arg=’expr $ # +1’
while [$ arg –le 3]
do
echo “Enter argument number $arg:”
read val
set $* $val
arg=’expr $arg +1’
done
fi
echo “Argument on command line entered by you are:”
echo $*
----------------
2.Write a program to create a thread by extending the thread class.
Answer-
import java.lang.*;
class Child extends Thread
{
Child()
{
super("Thread Demo");
System.out.println("Child Thread :" +this);
start();
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread
{
public static void main(String args[])
{
new Child();
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1500);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
---------(END)----------
-:Best Of Luck:-
1.Use the echo command to display the line
UNIX is fun to learn
Redirect the displayed line to a file. Append the outputs of the commands to show the users logged into your system, your login account and the current date in the dd-mm-yyyy format to the file.
Answer-
echo ”UNIX is fun to learn”
script –a myfile who –a whoami date +%d-%m-%Y
----------------
2.Write a program to print “ Welcome to Java Programming” on the screen.
Answer-
class welcome
{
public static void main(String a[])
{
System.out.println("Welcome to Java Progamming");
}
}
----------------
SET- 2
1. Assuming you have a directory containing the following files:
sprite, cola, choco, orange, book, lemon, lotus, apple
use the ls command to list only those files that
Answer-
a) consist of four letters
Answer- ls ????
b) begin with the letter c
Answer- c*.*
c) end with the letter e
Answer- *e.*
d) have the second letter o
Answer-?o.*
----------------
2.Write a program to check whether the given two numbers are equal. If not then find the greater of the given two numbers.
Answer-
import java.lang.*;
class Comparision
{
public static void main(String args[])
{
int x,y;
x=67;
y=97;
if(x==y)
{
System.out.println("Given two number are equal");
}
else if(x>y)
{
System.out.println(x+ " is greater than " +y);
}
else
{
System.out.println(y+ " is greater than " +x);
}
}
}
----------------
SET -3
1.For a file named myfile in the working directory, do the following:
a) give everyone permission to read myfile. No other privilege is to be changed
Ans :- chmod a+r myfile
b) allow the owner and group members to read and write the file. All privileges are to be removed for everyone else.
Ans:- chmod 660 myfile
c) remove write privileges from everyone except the owner
Ans:- chmod go-w myfile
d) allow the owner and group members to execute myfile and give only the owner permission to read or write to it.
Ans:- chmod 710 myfile , chmod ug=x,u+rw myfile
----------------
2.Using switch and case statements write a program to add, subtract , multiply and divide the two numbers.
Answer-
import java.io.*;
public class BasicCalc
{
public static void main(String a[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("What do you want to do? \n 1. Add, \n 2. Substract, \n 3. Multiply, \n 4. Divide");
int i = Integer.parseInt(in.readLine());
//int i=4;
int m, n;
m = 10;
n = 5;
switch (i)
{
case 1:
System.out.println("Result after Adding" + " " + (m + n));
break;
case 2:
System.out.println("Result after Substracting" + " " + (m - n));
break;
case 3:
System.out.println("Result after Multiplying" + " " + (m * n));
break;
case 4:
System.out.println("Result after Dividing" + " " + (m / n));
break;
default:
System.out.println("This is not a valid input parameter");
break;
}
}
}
----------------
SET -4
1. For a directory myfolder in your working directory, do the following:
a) allow everyone to list files in myfolder. No other privileges are to be changed.
Ans:- chmod a+rx myfolder
b) allow the owner to and group members to list, remove or add files. All privileges are to be removed from everyone else.
Ans:-chmod 770 myfolder
c) give write privileges to everyone except the owner
Ans:- chmod go+w myfolder
d) allow the owner and group members to execute myfolder. Only the owner gets read or write permission.
Ans:- chmod 710 my folder
----------------
2.Using for loop write a program to print the table of a given number on the screen.
Answer-
import java.util.Scanner;
public class Table
{
public static void main(String args[])
{
int num,i,res;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
num =s.nextInt();
for(i=1; i<=10; i++)
{
res=i*num;
System.out.println(res);
}
}
}
----------------
SET -5
1.Put a long-running process in the background and check the accuracy of the sleep command.
Answer-
date;sleep10;date$
----------------
2.Write a program to find the even sum and the odd sum in the given array.
Answer-
class EvenOddSum
{
static int sum1, sum2;
static int n[] ={ 2, 3, 4, 5, 6, 7, 92, 65, 45, 87 };
public static void main(String a[])
{
for (int i = 0; i <=n; i++)
{
if (n[i] % 2 == 0)
{
System.out.println(n[i] + " " + "*" + " " + "Even");
sum1 = sum1 + n[i];
}
else
{
System.out.println(n[i] + " " + "*" + " " + "Odd");
sum2 = sum2 + n[i];
}
}
System.out.println("sum of even no. is " + sum1);
System.out.println("sum of odd no. is " + sum2);
}
}
----------------
SET -6
1.The ls command with the –R option lists the files in the specified directory and also subdirectories, if any. Use this with a suitable pip-and-filter arrangement to determine whether a file exists in the account logged in.
Answer-
echo “Enter the name to search :”
read file
dat =’ls-R $ Home 1 grep $ file’
if [-s “$ dat”+
then
echo “$file File exist”
else
echo “$file File not exist”
fi
----------------
2.Write a program to print all the prime numbers between n and m.
Answer-
import java.io.*;
class PrimeNumber
{
public static void main(String[] args) throws Exception
{
int i;
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter 1st number: ");
int num = Integer.parseInt(bf.readLine());
System.out.println("Enter 2nd number: ");
int num2 = Integer.parseInt(bf.readLine());
System.out.println("Prime numbers are: ");
for (i = num; i <num2; i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i % j;
if (n == 0)
{
break;
}
}
if (i == j)
{
System.out.print(" " + i);
}
}
}
}
----------------
SET- 7
1.Write an interactive shell using I/O redirection to accept input from file1, put stdout in file2 and stderr in file3.
Answer-
echo “Enter input file name:”
read f1
echo “Enter output file name:”
read f2
echo “Enter error file name:”
read f3
cat <$f1 , cat >$f2 , cat >$f3
----------------
2.Write a program to add the two matrices.
Answere-
class MatrixSum
{
public static void main(String[] args)
{
int array[][]= {{4,5,6},{6,8,9}};
int array1[][]= {{5,4,6},{5,6,7}};
System.out.println("Number of Row = " + array.length);
System.out.println("Number of Column = " + array[1].length);
int l= array.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++)
{
for(int j = 0; j <= l; j++)
{
System.out.print(" "+ array[i][j]);
}
System.out.println();
}
int m= array1.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+array1[i][j]);
}
System.out.println();
}
System.out.println("Addition of both matrix : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+(array[i][j]+array1[i][j]));
}
System.out.println();
}
}
}
----------------
SET- 8
1.Write a shell program to extract and sort all the users in the file system. Use the /etc/passwd file as input.
Answer-
first (with pipe and filter)
Cut –d ”:” –f1/etc/passwd | sort
Second (without pipe and filter)
Cut –d “:” –f1/etc/passwd>tmp sort<temp
----------------
2.Write a class circle which consists of functions getdata() and area(), and also write a main program to create a circle object and to find the area by calling the function area () in circle class and display the result on the screen.
Answer-
import java.lang.*;
import java.io.*;
class Circle
{
double r,a;
static final double p=3.14;
public void getData()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of the circle :");
r=Double.parseDouble(br.readLine());
}
catch(Exception e)
{
}
}
public void area()
{
a=p*(r*r);
System.out.print("The area of the circle is :"+a);
}
public static void main(String args[])
{
Circle c=new Circle();
c.getData();
c.area();
}
}
----------------
SET- 9
1.Write a shell program to translate the /etc/passwd file into uppercase and translate the “:” delimiter into tab characters.
Answer-
tr ”*a-z:+” “*A-Z:+” </etc/passwd
----------------
2.Write a program to demonstrate the method overloading for sum ( ) function.
Answer-
class Demo
{
void add(int a)
{
System.out.println(a);
}
void add(int x, int y)
{
System.out.println(x + y);
}
public static void main(String args[])
{
demo q = new demo();
q.add(10);
q.add(20, 30);
}
}
----------------
SET- 10
1.Write a shell program using if-the-else to test whether a variable name is a directory or a file.
Answer-
echo “Enter a name”
read name
if [-f $name]
then
echo “$name is a file”
then
echo “$name is a directory”
else
echo “$name is not a file nor a directory.”
fi
----------------
2.Write a program to concatenate two strings.
Answer-
class Conct
{
public static void main(String a[])
{
String a1 = "Vikash";
String a2 = "Kumar";
System.out.println(a1.concat(" " + a2));
}
}
----------------
SET- 11
1.Write a shell program in which an infinite loop prompts a user for file names to be removed and remove the files. Use trap to exit when finished.
Answer-
trap ‘exit’ INT
While [true]
do
echo “Enter filename to be deleted (ctr+c to exit)”
read file
rm $file
done
----------------
2.Write a program to find whether a given string is a palindrome or not.
Answer-
class Palin
{
public static void main(String []s)
{
int i=0,j;
j=s[0].length()-1;
while(i<j)
{
if(s[0].charAt(i) != s[0].charAt(j))
break;
i++;
j--;
}
if(i<j)
System.out.println("Not palin");
else
System.out.println("Palin");
}
}
----------------
SET- 12
1. Write a background command to edit all the files in the directory replacing the word while with until.
Answer-
tr 'while' 'until' < myfile &
----------------
2. Write a program to change the case of the given string.
Answer-
import java.io.*;
class case
{
public static void main(String ar[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter any String");
System.out.println("and to quit write “ Exit“ ");
System.out.println("\n");
do
{
str = br.readLine();
int textLength = str.length();
for (int i = 0; i < str.length; i++)
{
char ch = Character.toUpperCase(str.charAt(i));
System.out.print(ch);
}
} while (!str.equals("exit"));
}
}
----------------
SET- 13
1.Write a shell program to test for arguments arg1, arg2 and arg3. If they are not present, prompt the user for them.
Answer-
if [$ # -lt 3]
then
arg=’expr $ # +1’
while [$ arg –le 3]
do
echo “Enter argument number $arg:”
read val
set $* $val
arg=’expr $arg +1’
done
fi
echo “Argument on command line entered by you are:”
echo $*
----------------
2.Write a program to create a thread by extending the thread class.
Answer-
import java.lang.*;
class Child extends Thread
{
Child()
{
super("Thread Demo");
System.out.println("Child Thread :" +this);
start();
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread
{
public static void main(String args[])
{
new Child();
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1500);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
---------(END)----------
-:Best Of Luck:-