0% found this document useful (0 votes)
21 views10 pages

Programming Study Notes

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

Programming Study Notes

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming Fundamentals

Complete Study Notes


C Language + Java Exam Preparation
PART 1: JAVA EXAM — Questions & Answers

Course: Object Oriented Programming | Instructor: Saad Ahmed | Total Marks: 15


Duration: 2 Hours | Attempt ALL questions

Q1 — Method Overloading (04 Marks)


Question: Write a program to demonstrate method overloading based on number of parameters.
Answer / Concept:
Method overloading means having multiple methods with the same name but different parameter lists in
the same class.
public class OverloadDemo {
// Method with 1 parameter
static void show(int a) {
[Link]("One param: " + a);
}
// Method with 2 parameters
static void show(int a, int b) {
[Link]("Two params: " + a + ", " + b);
}
// Method with 3 parameters
static void show(int a, int b, int c) {
[Link]("Three params: " + a + ", " + b + ", " + c);
}
public static void main(String[] args) {
show(5);
show(3, 7);
show(1, 2, 3);
}
}

Q2 — Command Line Arguments with Two Doubles (04 Marks)


Question: Write a Java program to find the sum of command line arguments passed as two double
numbers.
public class SumDoubles {
public static void main(String[] args) {
double a = [Link](args[0]);
double b = [Link](args[1]);
double sum = a + b;
[Link]("Sum = " + sum);
}
}
Run: java SumDoubles 3.5 2.1 → Output: Sum = 5.6
Q3 — Student Average using foreach loop (04 Marks)
Question: Write a Java program to input marks of 5 students and find the average using a foreach loop.
import [Link];
public class StudentAvg {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] marks = new int[5];
for (int i = 0; i < 5; i++) {
[Link]("Enter mark " + (i+1) + ": ");
marks[i] = [Link]();
}
double sum = 0;
for (int m : marks) { // foreach loop
sum += m;
}
double avg = sum / [Link];
[Link]("Average = " + avg);
}
}

Q4 — Program Output (03 Marks)


Question: Write the output of the following programs.

Part (a)
public class Test {
static int x = 10;
public static void main(String[] args) {
int x = 20;
[Link](x); // Output: 20 (local variable)
}
}
Output: 20 (local variable shadows the static variable)

Part (b) — Based on visible code pattern


public class Test2 {
public static void main(String[] args) {
int[] a = {1, 2, 3};
int b = a;
b = 10;
[Link](a[0]); // Output: 1
}
}
Output: 1 (primitive copy, original array unchanged)
PART 2: C LANGUAGE — Chapter 1 Notes

Introduction to C Language
History
• C was developed by Dennis Ritchie at Bell's Lab in 1972
• Before C: GW Basic, Pascal, Fortran were used
• C quickly became the most popular programming language

Features of C Language
• Middle-level language (combines high and low level features)
• Case-sensitive language (int ≠ INT)
• Uses a compiler as a language translator

IDEs for Writing C Programs


• TURBO C++ — used initially in this course
• DEVC++
• VISUAL C++

Your First C Program


#include <conio.h>
#include <stdio.h>
void main() {
clrscr();
printf("Welcome to C-Language");
getch();
}

Explanation of First Program


Element Meaning
# Preprocessor directive

include Name of directory command

<conio.h> Console Input/Output header file

<stdio.h> Standard Input/Output header file

void main() Entry point of every C program


clrscr() Clears the output screen

printf() Displays text on output screen

getch() Holds screen until user presses a key

Compile & Execute Shortcuts


• ALT + F9 = Compile only
• CTRL + F9 = Compile AND Run
• F2 = Save file
• Default save location: C:/TC/BIN/
• Default file extension: .CPP

Escape Sequences
Sequence Meaning
\n New Line

\t Horizontal Tab

\r Carriage Return

\" Double Quote

\' Single Quote

\a Alert / Bell

\b Backspace

\\ Backslash

Other Useful Functions


• gotoxy(int x, int y) — moves cursor to position (x,y) on screen
• delay(int ms) — pauses for given milliseconds (needs dos.h)
• sleep(int sec) — pauses for given seconds (needs dos.h)
• textcolor(int color) — changes text color (needs graphics.h)
• textbackground(int color) — changes background color

Comments in C
• Single line: // This is a comment
• Multi-line: /* This is a multi-line comment */
PART 3: Data Types, Variables & Constants

Variables in C
Rules for Variable Names
• First character must be alphabetic [a-z, A-Z] or underscore (_)
• Can only contain letters, digits, and underscores
• Cannot be a reserved word (int, float, etc.)
• Case-sensitive: 'Sum' and 'sum' are different

Valid vs Invalid Identifiers


Valid Invalid
sum 7of9 (starts with digit)

c4_5 x-name (hyphen not allowed)

A_NUMBER name with spaces

_split_name int (reserved word)

TRUE AXYZ& (special char)

Variable Declaration Syntax


Type Name;
int sum;
float avg;
char dummy;
int x, y, z; // multiple at once
int x = 0; // with initialization

Data Types in C
Basic (Atomic) Types
Type Bytes Bits Min Value Max Value
char 1 8 -128 127

short int 2 16 -32,768 32,767

int 4 32 -2,147,483,648 2,147,483,647

long int 4 32 -2,147,483,648 2,147,483,647

float 4 32 3.4e-38 3.4e+38


double 8 64 1.7e-308 1.7e+308

long double 10 80 smaller larger

Types of Data
• Numeric: Integer (whole numbers) and Real/Float (decimals)
• Character/String: Letters, digits, special chars in quotes
• Logical: TRUE/FALSE, YES/NO, 1/0

Printf/Scanf Format Specifiers


Data Type printf specifier scanf specifier
int %d %d

float %f %f

double %f %lf

long double %Lf %Lf

char %c %c

short %hd %hd

long int %ld %ld

unsigned int %u %u

Constants in C
• Literal constants: fixed values like 5, 3.14, 'A'
• #define MAX_NUMBER 100 — preprocessor defined constant
• const float PI = 3.14159; — memory constant (cannot be changed)

Character Encoding
• ASCII: American Standard Code for Information Interchange
• Standard ASCII: 7 bits = 128 characters
• Extended ASCII: 8 bits = 256 characters
• EBCDIC: IBM's alternative (incompatible with ASCII)
• Unicode: International standard, version 16.0 has 155,063 characters

Basic Input/Output Example


#include <stdio.h>
int main() {
int var;
printf("Enter an integer: ");
scanf("%d", &var); // & is address-of operator
printf("You entered: %d\n", var);
return 0;
}
QUICK REVISION CHECKLIST

Java Exam Key Points


• Method Overloading = same name, different parameters
• Command line args accessed via String[] args, convert with [Link]()
• foreach syntax: for (int x : array) { }
• Local variables shadow class/static variables

C Language Key Points


• C developed by Dennis Ritchie, 1972, Bell Labs
• Every C program needs main() as entry point
• #include <stdio.h> for printf/scanf
• scanf uses & (address-of operator), printf does NOT
• %d=int, %f=float, %c=char, %lf=double
• ALT+F9 compile | CTRL+F9 run | F2 save
• Escape: \n newline, \t tab, \\ backslash

You might also like