IB Computer Science B2.1: Programming fundamentals
New IB Computer Science syllabus · first exams May 2027 · Theme B · Paper 2 · SL & HL · Python and Java
B2 Programming is the largest unit in the course (40 hours SL, 42 hours HL) and the core of Paper 2, which is written in real code, in Python or Java. B2.1 covers the basics every program needs.
| Statement | Topic | Command term |
|---|---|---|
| B2.1.1 | Global and local variables of various data types | Construct / Trace |
| B2.1.2 | Extracting and manipulating substrings | Construct |
| B2.1.3 | Exception handling | Describe |
| B2.1.4 | Debugging techniques | Construct / Use |
Key points
B2.1.1 Variables and data types
- The five types named in the guide:
- Boolean
- char: a single character in Java; a 1-character string in Python
- decimal:
floatin Python,doublein Java - integer
- string
- Scope:
- Local variables exist only inside their function.
- Global variables are visible everywhere. In Python, you must write
globalinside a function to change one.
- Be ready to trace how values change across function calls.
B2.1.2 Substrings
Find, extract, alter, concatenate and replace parts of a string. Python uses slicing (s[a:b]) and find(); Java uses substring() and indexOf().
B2.1.3 Exception handling
- Three failure points the guide names:
- unexpected inputs
- unavailable resources, such as a missing file
- logic errors
- Python:
try/except/finally - Java:
try/catch/finally
The finally block always runs, so use it to close resources and tidy up.
B2.1.4 Debugging
- Trace tables
- Breakpoints
- Print statements
- Stepping through the code line by line
Each one reveals where the actual values differ from what you expected.
Exam-style question
Construct code, in either Python or Java, that extracts the domain (the part after the "@") from the string "student@cswithrazia.com" and outputs it. [4]
Mark scheme
[2] for correct index logic, [2] for correct output.
Python
email = "student@cswithrazia.com"
domain = email[email.find("@") + 1:]
print(domain) # cswithrazia.comJava
String email = "student@cswithrazia.com";
String domain = email.substring(email.indexOf("@") + 1);
System.out.println(domain); // cswithrazia.comCommon mistakes that cost marks
- Off-by-one errors in substrings. The
+ 1above skips the "@" itself. - Changing a global in Python without
global. Python then creates a new local variable instead. - Writing
exceptwith no specific error, and nofinally, when the question asks you to manage a resource such as a file.
Frequently asked questions
Can I answer Paper 2 in Python or Java?
Yes. Paper 2 is available in parallel Python and Java versions. Learn one language thoroughly.
Do I need both try/except and try/catch?
You need the one for your language: try/except in Python, or try/catch in Java, plus the finally block in either.
Go further
- Practise B2.1 with instant marking on ExamHaus, in Python or Java. → examhaus.com
- Full B2.1 presentation and question bank with mark scheme on CS Exam Ready. → csexamready.com/resources/ib-diploma
- IB CS mock exams: Paper 2 in Python and Java. → examhaus.com/mock-exams
