MidSemester Examination 1
Esc101 Introduction to Computing [40 marks, 60 minutes]
YOUR NAME ____________________________ ROLL NUMBER __________________
1. Short quesions (15 points)
a. Give two expansions for the word "LISP", which is one of the earliest programming languages.
[2]
b. James __________, of Sun Microsystems, was one of the key architects of the Java programming language. [1]
c. The smallest number of type short is: __________ This is approximately equal to ___________ in the decimal system. [2]
d. What is the equivalent do-while loop for the loop: [3]
for (; ;) {<statement>}
e. Consider this fragment using a class QuizClass: [3]
QuizClass a = new QuizClass(10);
// initializes a with marks 10
QuizClass b = a;
b.setMarks(3); // sets b's marks to 3
what are the marks of a and b at the end?
f. What is the value of w after line 2 is executed? [2]
(Remember Java evaluates expressions from left to right).
int x=3, y=4, z=5, w;
w=x*(y=x+z)*(z=y-2*x) + (++y); // line 2
g. Comment on the programming style of q.(f) above. [1]
h. The statement
char c;
allocates how much memory to the variable "c"? [1]
2. Consider the following function defined in some class. (9 Points)
static void f1(int n)
{
for (int i=1;i<=n;i++) System.out.print(" ");
}
static void f2(String pat,int n)
{
for (int i=1;i<=n;i++) System.out.print(pat);
}
static void func(int n)
{
for (int i=1;i<=n;i++)
{ f1(n-i);
f2("a ",i);
System.out.println();
}
for (int i=n-1;i>0;i--)
{ f1(n-i);
f2("a ",i);
System.out.println();
}
}
a. What are the functions f1, f2 and func doing? Give short
answers. [1+1+3]
[Remember: System.out.print does
not insert a carriage return after priinting.]
f1:
f2:
func:
b. Indicate the result of calling func with an argument 4. [4]
3. Here you are expected to consider the class Paragraph: (16 points)
/**
Handles Paragraphs of text.
*/
public class Paragraph
{
// each element in this array is a line in a paragraph.
private String[] para;
// ---- line A -----
public Paragraph(String[] p)
{
para = new String[p.length];
for(int i=0; i<p.length; i++)
{ para[i] = makeNewCopy(p[i]);
// this method makes a copy of the string p[i]
}
}
// ---- line B -----
public String toString()
{
String str = "";
for(int i=0; i<para.length; i++)
str += para[i] + "\n";
return(str);
}
public static void main(String args[])
{
String[] str =
{
"We pass examinations, and shrivel up into clerks,",
"lawyers and police inspectors, and we die young.",
"... Once upon a time we were in possession of such",
"a thing as our mind in India. But it has been",
"thrust aside, and we are made to tread the mill of",
"passing examinations. - Tagore" ,
};
Paragraph pp = new Paragraph(str);
// ---- line C -----
System.out.println(pp);
str[0] = "ABCDEFG" ;
str[1] = "Ismensey nikley pandit-ji" ;
str[2] = " - schoolchildren's couplet,";
str[3] = " possibly referring to Nehru's ";
str[4] = " English speeches";
// ---- line D -----
System.out.println(pp);
String[] str2 = { "Old Pond", "Frog jumps in", "Sound of water"};
str = str2;
// ---- line E -----
System.out.println(pp);
}
}
a. Insert a javadoc comment at line A [2]
b. What is the output at Lines C, D and E?
[Refer to a paragraph by the first and last lines;
If there is an error near any of these lines, please indicate so]
[6]
C:
D:
E:
c. Consider now the situation where ALL THE LINES from line A to line B are changed; they now read: [6] [6]
Paragraph(String[] p)
{
para = p;
}
What happens now at lines C, D and E?
C:
D:
E:
d. What happens if at line E we insert the following code: [2]
pp.para[3] = "hello";