Question 3
A. Refer to the code segment below and answer the questions in the comments labelled Question i – vi. For each answer; provide a brief explanation for your answer.
public class Scope
{
private int x = 2;
private int x = 1;
public void start()
{
int x = 5;
int x = 2;
//Question i
System.out.printf( "The value of x is %d\n", x );
int x = 2;
//Question ii
System.out.printf( "\nThe value of x is is %d\n", x );
} // end method begin
4 public void middle()
{
int x = 25;
//Question iii
System.out.printf( "\nThe value of x is %d\n", x );
++x;
//Question iv
System.out.printf( "The value of x is %d\n", x );
} // end method middle
public void end()
{
//Question v
System.out.printf( "\nThe value of x is %d\n", x );
x *= 10;
//Question vi
System.out.printf( "The value of x is %d\n", x );
} // end method end
} // end class Scope
Question 4
A) Write a shell script that does the following
Allows the user to enter seven urls into a text file.
Check to see the status of the links as in whether they are working or not
Write the name of the url and it’s status into another file.
Append to the file how many urls are working and how many are broken.
[10 marks]
B) With a working example show how IO Redirection works in shell scripting.
[7marks]
C) With examples show how each of these external commands work
Awk
Sed
Grep
[4 marks]
D) Why would this process give an error
1) first create the file
touch my_example.sh
2) open it in an editor
gedit my_example.sh
3) write some code
#!/bin/sh
echo “Shell scripting is fun.”
4) run the file
./my_example.sh
Question 3
Write a shell script that allows a user to write a nursery rhyme directly from the CLI and have the rhyme stored in a text file.
Your script should then replace all vowels in the rhyme with the letter “t”
Your script should also count the number of lines in the rhyme
Your script should make a copy of the text file and append all the characters of the first file into the second file into it.
Then delete all the contents of the first file.
[9 marks]
4
B) Give four working examples of how to use grep in shell scripting,
[6 marks]
C) list all hidden files in the present working directory showing all hidden files and in the long format
[6 marks]
D) Explain the difference between ls -l > mylisting.txt and ls -l >> mylisting.txt
[4 marks]
Question 2
A) The AIT canteen wants you to write a script for them which would do the following
I) receive records from the user and keep these records into a file, the records must include
ID
Name of meal
Price of meal
Quantity bought today
Sales made for the day
3
Today the sales person wants to make seven entries into the file via the command line interface.
Your script should be able to find to present the sales person with the ability to
Search for a particular meal by ID
Search for a particular meal by name
find out how much money has been made from the sales of a particular meal
list all records
list all meals and their prices
list a particular meal and its price
[10marks]
B) With a shell script explain how to use case
[9marks]
C) With code explain how the following work
if statements
if else statements
if elif else fi statements
[4 marks]
D) Explain the difference between= grep -i "some string" some file and
grep -iw "some string" some file
Write a python program that:
Question 1
A) Write a game entirely in shell script which generates an unknown number and prompts the player to enter a number. Your game should receive the number and compare it to the generated number, let your game tell the player whether the number entered is higher than or lower than the hidden number until the player is able guess the number or quit the game, upon guessing the right number the game should congratulate the player for winning and tell the player how many attempts were made before guessing the right number.
[10 marks]
B) Write a shell script that uses a function to count the number of files in the present working directory and output their number to the screen.
[10marks]
C)Explain with examples how to make variables available to child processes/shells
A.) A module is required to enable lecturers to easily identify the highest and lowest performing students in their courses. As the lead programmer, write a java program to achieve this. Your program should have the following features: Each correctly implemented feature earns you 3 marks.
i. The program should make use of a sentinel to control the number of grades entered.
ii. Total Number of grades entered
iii. The average of the grades
iv. Number of students that achieved a grade of 80 and above
v. Number of students that achieved a grade of 50 and below
B.) An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute. Write an algorithm for the implementation of the sentinel control in Question A Sub Question I above.
C.) For larger datasets, a linear search may be inadequate or perhaps inefficient. What other search method can be employed to return the maximum number from a set of elements in an array. Explain your answer.
A. Write a java program that reads three (3) int variables and performs the below operations: i. Prints out their product ii. Prints out the maximum of the numbers Do not use any inbuilt Java Methods to return the maximum. Extra marks will be awarded for clarity of code and comments. [8 marks] B. Algorithms perform differently on different data sets and as such we may be interested in either their Best-case, Worst-case or Average-case scenarios. Explain why the Worst-case for an insertion sort is with reverse-sorted data. [6 marks] C. Identify and correct the errors in each of the following sets of code: [6 marks]
i. while ( c <= 5 ) {
product *= c;
++c;
ii. if ( gender == 1 )
System.out.println( "Woman" );
else;
System.out.println( "Man" );
iii. What is wrong with the following while statement?
while ( z >= 0 ) sum += z;
D. Explain two (2) programming situations that will require the use of overloaded methods [5 marks]
The class employee comprises of employee details such as emp_id(integer type), employee name(character array type) ,basic salary(float type), allowance(float type) and PF(float type). Get three employee details using getemployee() method and use display() method to calculate and display the net salary(basic salary+allowance+PF) of employees.
Note:Use the concept of class and objects.
(ii)Explain two (2) programming situations that will require the use of overloaded methods [5 marks]