Monday, 20 January 2014

Basic Questions and Answers

1.       A man bought an item for N5000 and sold it at a loss of 24%. Write a basic program to compute the item selling price.
ANSWER
REM program to compute item selling price
LET costP = 5000
Loss = (24/100)*costP
SellingPrice = costP – Loss
PRINT SellingPrice
End
2.       Describe the output that will be generated by the BASIC program below
LET x = 0
FOR I = 0 to 4
     FOR j = 0 to i-1
           LET k = (I + j -1)
           IF(k MOD 2 = 0) THEN
                LET x = x + k
           ELSEIF(k MOD 3 = 0)
     THEN
           LET x = x + k – 2
           ENDIF
           PRINT x
     NEXT j
NEXT i
END



ANSWER
1
J
X
0
0
NOT EXECUTABLE
1
0
0
2
0
0
2
1
2
3
0
4
3
1
5
3
2
9
4
0
10
4
1
14
4
2
20
4
3
20

3.       State the features that lead to the development of micro-computer and discuss the major components of one of the features
ANSWER
-          VLSI referred to as microprocessor
-          Semiconductors memories
-          Enhanced high level languages and operating systems
-          Optimization techniques in compiler
4.       What are the distinctions between RAM and ROM and discuss the disadvantages of primary memory that led to the introduction of secondary memory
ANSWER
Information stored in RAM cannot be retained permanently while that of ROM is not, RAM is the working memory while the content of ROM is used in carrying out power on switch test.
The disadvantages of primary memory is the limited storage capacity and cant retain information permanently

5         If you are given three sticks, you may or may not be able to arrange them in a triangle. For any three lengths “if any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can.” Write a program that takes three integers as inputs and determine whether you can or cannot form a triangle from sticks with the given lengths.
ANSWER
INPUT “Enter the first length”, first
INPUT “Enter the second length”, second
INPUT “Enter the third length”, third
IF (first + second) < third THEN
     PRINT “you cannot form a triangle”
ELSE IF (first + third) < second THEN
     PRINT “you cannot form a triangle”
ELSE IF (second + third) < first THEN
     PRINT “you cannot form a triangle”
ELSE
     PRINT “Triangle can be formed”
END IF
END

Python Questions and Answers


If you are given three sticks, you may or may not be able to arrange them in a triangle. For any three lengths “if any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can.” Write a program that takes three integers as inputs and determine whether you can or cannot form a triangle from sticks with the given lengths.
Answer
#program to test if 3 sticks can perfectly form a triangle
#inputs:

a = float(raw_input(“Enter the length of side A?”))
b = float(raw_input(“Enter the length of side B?”))
c = float(raw_input(“Enter the length of side C?”))

if (a > (b + c)):
     status = False;
elif (b > (a + c)):
     status = False;
elif (c > (a + b));
     status = False;
else:
     status = True;
if (status):
     print “A perfect triangle can be formed”. Format (a,b,c)
else:
     print “A perfect triangle cannot be formed” Format(a,b,c)


  The usual definition of a palindrome is a word that reads the same both forward and backward like “otto”, “ada” and “madam”. Therefore we might say, a single letter is a palindrome and a two-letter word is a palindrome if the letters are the same and any other word is a palindrome if the first letter is the same as the last and the middle is a palindrome. Write a program to read a string and indicate whether the word is palindrome or not.
ANSWER
#program to test if an input string is a PALINDROME
#method to reverse the string
def reverse(string):
     new_string = “”
     for char in string:
           new_string = char + new_string
     return new_string
#method to check if the string is a palindrome
def isPalindrome(string):
     flag = True;
     if (len(string) > 0);
           reverse_string = reverse(string)
           for index in range(0,len(string));
                if(string[index] != reverse_string[index]):
                     flag = False
                     break
                else:
                     flag = True
     else:
           print “String must not be empty!”
     return flag
#program section to read an input string
#and then test if it is a Palindrome

text = raw_input(“Please type in a string…..?”)
if (isPalindrome(text)):
     print text, “is a PALINDROME!!!!!”
else:
     print text, “is not a PALINDROME!!!!!”

          A word is said to be “abecedarian” if the letters in the word appear in the alphabetical order. For example, the following letters are all  6-letters English abecedarian words: abdest, acorsy, adempt, agnosy, best, behint, beknow, bijoux, biopsy, chintz, deux, dehort, write a program for checking whether a given word is abecedarian, assuming that the word contains only lower-case letters.
ANSWER
#program to test if an input string is ABECEDARIAN
#method to get the list of ascii codes of all characters in the string
def get ascii_list(string):
     asciis = []
     for char in string:
           asciis.append(ord(char))
     return asciis
#method to check if the string is a ABECEDARIAN
def isAbecedarian(string):
     flag = True;
     if(len(string) > 0):
           ascii_codes_list = get_ascii_list(string)
           min_code = ascii_codes_list[0]
           for codein ascii_codes_list:
                if(min_code > code):
                     flag = True
                     min_code = code
                else:
                     print”string must not be empty!”
                return flag
#program section to read an input string
#and then test if it is ABECEDARIAN

text  = raw_input(“Please type in a string….?”)
if(isAbecedarian(text)):
     print text, “is a ABECEDARIAN!!!!”
else:
     print text, “is not a ABECEDARIAN!!!!”

          
          
          State 4 major features of the Python Programming Language.
1…………………………………………………………………………………………………………………….
2…………………………………………………………………………………………………………………….
3…………………………………………………………………………………………………………………...
4……………………………………………………………………………………………………………………
ANSWER
Python is open source
Python is interactive, user-friendly and easy to learn
Python is platform independent
Python is object-oriented
Python is extensible
Python is a dynamic language
Python supports multiple programming languages paradigms
Python has a large library of codes
Python has a large user support community
Python is an interpreted high-level language.

    

      Describe the functions of the following built-in functions with an example each
(a)    raw_input():
Description: is a python function that can be used to read in user inputs from the keyboard
Example: name = raw_input(“what is your name?”) #reads a name

(b)   round()
Description: is a function used for rounding up a floating-point number to a given number of precision
Example: val = round(34.56778, 2) #rounds to 2 dec places

(c)    chr()
Description: is a function used for an ASCII code number to its corresponding ASCII character.
Example: char = chr(65) #converts 65 to ascii character which is ‘A’

(d)   range(n,k)
Description:  a function used to generate a list of numbers between the range of N up to K-1
Example: numbers = range(1, 11) #generates the list {1,2,3,4,5,6,7,8,9,10}