Week 2
Look at the following class definition:
public class Circle extends Shape {
private float radius;
private static int num = 0;
public Circle(int xPos, int yPos) {
this.xPos = xPos;
this.yPos = yPos;
num++;
}
}
What is the value of num after the following code segment?
for (int cnt = 2; cnt < 5; cnt++)
circles[cnt] = new Circle(cnt, cnt + 2);
Examine the following method:
private void printThis(char ch, int num) {
int i, j;
for(i = 1; i <= num; i++) {
for(j = 0; j < num - i; j++)
System.out.print(" ");
for (j = 0; j < i; j++)
System.out.print(ch);
System.out.println();
}
}
What is the effect of calling the method (from within the same class) as follows:
printThis('*', 2);
Week 3
Question1:
What is the output of running the following code?
#include <stdio.h>
int y = 1;
void f(int a)
{
a += y;
y += a;
}
int main()
{
int x = 0;
f(x);
printf("%d %d\n", x, y);
return 0;
}
What is the output of running the following code?
#include <stdio.h>
int a = 0;
void f()
{
int a = 1;
int b = 2;
{
int b = 3;
int c = 4;
{
int c = 5;
printf("%d %d %d", a, b, c);
}
}
}
int main()
{
f();
return 0;
}
Given the following segment of code:
int colour;
int *myColour;
myColour = &colour;
colour = 0;
/* Insert statement here! */
Which one of the following is reasonable output for the statement:
printf("%d, %d", &colour, myColour);
inserted at the place indicated in the code above?
Given the following segment of code:
int colour;
int *myColour;
myColour = &colour;
colour = 0;
/* Insert statement here! */
Which one of the following statements will print the rvalue of 'myColour'?
The following diagram represents the contents of a small
segment of memory. The variable 'ptr' initially points to the first
'cell' of memory in the diagram. The first 'cell' is found at memory
location 1000.
| 'H' | 'e' | 'l' | 'l' | 'o' | '!' | \0 | 'W' | 'o' | 'r' | 'l' | 'd' | '!' | \0 | '!' |
What is printed by the following code segment?
char *ch;
ch = ptr+5;
while (*ch)
putchar(*ch++);
The following diagram represents the contents of a small
segment of memory. The variable 'ptr' initially points to the first
'cell' of memory in the diagram. The first 'cell' is found at memory
location 1000.
| 'H' | 'e' | 'l' | 'l' | 'o' | '!' | \0 | 'W' | 'o' | 'r' | 'l' | 'd' | '!' | \0 | '!' |
What is printed by the following statement?
printf("%s %s", ptr+5, ptr+12);
Select the most accurate description of the effect of the following UNIX command:
mv File/fred File/freda
Select the most accurate description of the effect of the following UNIX command:
cp ./test fred/
Look at the following code:
typedef struct Node
{
int val;
struct Node *next;
} Node;
Write some code that properly deletes all negative-valued elements of a linked list of 'Node' elements, pointed to by 'ptr'.
Week 10
Write some C code that will read words (one per line) from standard input.
Define a suitable structure that will hold each word and its length. Words canWrite a shell script that: