Hungarian notation for variables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Hungarian notation for variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which variable name uses Hungarian notation for a whole number holding a student's age?
A intAge
B age
2. What data type does the variable strSurname most likely hold?
A A string (text)
B A Boolean (True/False)
3. Which name is the better choice for a decimal price such as 4.95?
A intPrice
B fltPrice
4. A variable holds True or False to show whether a student passed. Which name follows Hungarian notation?
A strPassed
B blnPassed
5. Which name correctly combines a Hungarian prefix with camel casing?
A strFirstName
B strfirstname

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1strName AThe int prefix says integer, but the value is a string. The prefix should be str, giving strName.
2blnPassed BintStudentAge
3A whole-number variable named with a Hungarian notation prefix, then camel casing for the rest of the name CThe prefix starts with a capital letter. Hungarian notation prefixes must be lower case, so it should be strName.
4A decimal-number variable named with a Hungarian notation prefix and a descriptive word DA variable holding a Boolean (True or False) value, shown by the bln prefix
5StrName = "Ava" EA variable holding a string (text), shown by the str prefix
6intName = "Liam" FfltPrice

Definitions

Hungarian notation

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___Name = "Ava"
2.
___Age = 16
3.
___Price = 4.50
4.
___Passed = True
5.
marks = 72 ___HasPassed = marks >= 50

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A strName = input("Name: ")
B intAge = int(input("Age: "))
C intPrice = 12.50
D blnPassed = True
Question 2
A strCity = "Geelong"
B strPopulation = 270000
C print(strCity, intPopulation)
Question 3
A strScore = 72
B blnPassed = intScore >= 50
C print(blnPassed)
Question 4
A strFirstName = "Mia"
B strLastName = "Chen"
C intYear = 11
D average = 78.5
E print(strFirstName, fltAverage)
Question 5
A fltPrice = 4.50
B blnQty = 3
C fltTotal = fltPrice * intQty
D print(fltTotal)

Answer Key

Hungarian notation for variables
Section A: Quick Questions
Q1
A — intAge
Q2
A — A string (text)
Q3
B — fltPrice
Q4
B — blnPassed
Q5
A — strFirstName
Section B: Match the Pairs
Q1
strName → A variable holding a string (text), shown by the str prefix
Q2
blnPassed → A variable holding a Boolean (True or False) value, shown by the bln prefix
Q3
A whole-number variable named with a Hungarian notation prefix, then camel casing for the rest of the name → intStudentAge
Q4
A decimal-number variable named with a Hungarian notation prefix and a descriptive word → fltPrice
Q5
StrName = "Ava" → The prefix starts with a capital letter. Hungarian notation prefixes must be lower case, so it should be strName.
Q6
intName = "Liam" → The int prefix says integer, but the value is a string. The prefix should be str, giving strName.
Section C: Definitions
Q1
Hungarian notation — A naming style where a variable name starts with a short lower-case prefix showing its data type, e.g. strName, intAge, fltPrice, blnPassed.
Section D: Fill in the Blanks
Q1
strName = "Ava"
Q2
intAge = 16
Q3
fltPrice = 4.50
Q4
blnPassed = True
Q5
marks = 72 blnHasPassed = marks >= 50
Section E: Spot the Error
Q1
C — The value 12.50 is a floating point number, so the prefix should be flt, not int. The line should be fltPrice = 12.50.
Q2
B — 270000 is a whole number, so the prefix should be int, not str. The line should be intPopulation = 270000.
Q3
A — 72 is an integer, so the prefix should be int. The str prefix wrongly suggests text. The line should be intScore = 72.
Q4
D — Hungarian notation needs a data type prefix at the start of the name. This float variable is missing its flt prefix. It should be fltAverage = 78.5.
Q5
B — The bln prefix is for Boolean values (True or False). The quantity 3 is an integer, so the name should be intQty.